Background
Use the threadstart delegate thread to listen for socket traffic and call Savetrainresult commit after the communication is complete
now the question
the socket communication succeeds and the data parsing succeeds, but the savetrainresult method Times exception information is called :
System.Net.Sockets.SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall .
At System.Net.Sockets.Socket.Accept () [0x00000] in <filename unknown>:0
At Socketserver.listen () [0x00000] in <filename unknown>:0
The relevant code is as follows:
1. Using socketserver.startlistening () to start the socket (TCP) service and listen monitoring
1 public static string data = NULL;
2 private static Socket listener=null;
3 Public StaticThread MyThread =NULL;4 Private StaticSocket worksocket=NULL;5 6 /// <summary>7 ///Open Socket8 /// </summary>9 /// <returns>The listening.</returns>Ten Public Static Objectstartlistening () { One loadinitfile.initclientparams (); A -IPAddress IPAddress =Ipaddress.parse (LOADINITFILE.CLIENT_IP); - theIPEndPoint Localendpoint =NewIPEndPoint (IpAddress,3001); - //Create A TCP/IP socket. - if(Listener = =NULL) { -Listener =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); + Listener. Bind (localendpoint); -Listener. Listen (Ten); + } A - - Try{ -ThreadStart mythreaddelegate =NewThreadStart (Listen); - //instantiating a new thread inMyThread =NewThread (mythreaddelegate); - toMythread.isbackground =true; + Mythread.start (); - the}Catch(Exception e) { *Log.log ("Thread Exception:"+e.tostring ()); $ }Panax Notoginseng - return 1; the } + A Private Static intSleeptime= +; the Private Static stringCasemsg =""; + Public Static voidListen () { - //data buffer for incoming data. $ byte[] bytes =New byte[1024x768]; $ Try { -worksocket=Listener. Accept (); - while(true) { thedata =NULL; - intMsglen =0;Wuyi intBytesrec =worksocket.receive (bytes); thedata = Encoding.UTF8.GetString (bytes,0, Bytesrec); -Looping through data
.... the if(!string. IsNullOrEmpty (data)) { About receivemsg (data); the Break; the } +worksocket=NULL; the } Bayi}Catch(Exception e) { the Log.log (e.tostring ()); the}finally{ - closesocketserver (); - preparationworkfun prs=new preparationworkfun (); the PRS. Savetrainresult (); the } the}
2, Closesocketserver
1 /// <summary>2 ///Close Socket3 /// </summary>4 Public Static voidClosesocketserver () {5 Try{6 7 if(Listener! =NULL) {8 Listener. Shutdown (Socketshutdown.both); 9 Listener. Close ();Ten } One}Catch(SocketException e) { A Listener. Close (); -}finally{ -Listener=NULL; the } -}
3, Preparationworkfun.savetrainresult
1 New datastoserver (); 2 startcoroutine (Datatoserver.savetrainresult (Datasfromserver.currenttrainresult));
4.
Datatoserver.savetrainresult
1 PublicIEnumerator Savetrainresult (trainresultentity tre) {2 if(Tre = =NULL){3 yield return 0;4 }5 6 stringPath =Datasourceutil.savetrainresultpath (tre);7 if(string. IsNullOrEmpty (Path)) {8 yield return 0;9 }Ten Onewww www =NewWWW (path); A yield returnwww; - - if(WWW! =NULL) { the - if(!string. IsNullOrEmpty (Www.error)) { - -Debug.Log ("The results of the training test failed to save! "+www.error); + - www.Dispose (); + Awww =NULL; at}Else{ -Debug.Log ("The results of the training test were saved successfully! "+www.text); -DatasFromServer.CurrentTrainResult.Id =int. Parse (Www.text); - } - } -www =NULL; in -}
Issue: System.Net.Sockets.SocketException: A blocking operation was interrupted by a call to WSACancelBlockingCall.