问题:System.Net.Sockets.SocketException: 一个封锁操作被对 WSACancelBlockingCall 的调用中断。

时间:2016-01-15 16:04:00   收藏:0   阅读:4387

背景

使用ThreadStart委托线程监听socket通信,在通信完毕后调用saveTrainResult提交信息

现在的问题

socket通信成功且数据解析成功,但在调用saveTrainResult方法时报异常信息:

System.Net.Sockets.SocketException: 一个封锁操作被对 WSACancelBlockingCall 的调用中断。

 

  at System.Net.Sockets.Socket.Accept () [0x00000] in <filename unknown>:0 

  at SocketServer.Listen () [0x00000] in <filename unknown>:0

相关代码如下:

1、使用SocketServer.StartListening () 启动socket(TCP)服务与Listen监听

 1 public static string data = null; 
2 private static Socket listener=null;
3
public static Thread myThread = null; 4 private static Socket workSocket=null; 5 6 /// <summary> 7 /// 开启socket 8 /// </summary> 9 /// <returns>The listening.</returns> 10 public static object StartListening(){ 11 LoadInitFile.initClientParams (); 12 13 IPAddress ipAddress = IPAddress.Parse (LoadInitFile.Client_Ip); 14 15 IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 3001); 16 // Create a TCP/IP socket. 17 if (listener == null) { 18 listener = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 19 listener.Bind (localEndPoint); 20 listener.Listen (10); 21 } 22 25 26 try{ 27 ThreadStart myThreadDelegate = new ThreadStart (Listen); 28 //实例化新线程 29 myThread = new Thread (myThreadDelegate); 30 31 myThread.IsBackground = true; 32 myThread.Start (); 33 34 }catch(Exception e){ 35 log.log("Thread exception:"+e.ToString()); 36 } 37 38 return 1; 39 } 40 41 private static int sleepTime=1000; 42 private static string caseMsg = ""; 43 public static void Listen() { 44 // Data buffer for incoming data. 45 byte[] bytes = new byte[1024]; 46 try { 47 workSocket= listener.Accept (); 48 while (true) { 49 data = null; 50 int msgLen = 0; 51 int bytesRec = workSocket.Receive (bytes); 52 data = Encoding.UTF8.GetString (bytes, 0, bytesRec); 53  //循环读取数据
         .... 73 if (!string.IsNullOrEmpty(data)) { 74 ReceiveMsg (data); 75 break; 76 } 78 workSocket=null; 80 } 81 } catch (Exception e) { 82 log.log (e.ToString ()); 83 }finally{ 84 CloseSocketServer(); 85 PreparationWorkFun prs=new PreparationWorkFun(); 86 prs.SaveTrainResult(); 87 } 88 }

2、CloseSocketServer

 1 /// <summary>
 2 ///  关闭socket
 3 /// </summary>
 4 public static void CloseSocketServer(){
 5     try{
 6             
 7         if (listener != null) {
 8             listener.Shutdown(SocketShutdown.Both);    
 9             listener.Close();
10         }
11     }catch(SocketException e){
12         listener.Close();
13     }finally{
14         listener=null;
15     }
16 }

3、PreparationWorkFun.SaveTrainResult

1 DatasToServer dataToServer = new DatasToServer();
2 StartCoroutine(dataToServer.SaveTrainResult(DatasFromServer.CurrentTrainResult));

 

4、dataToServer.SaveTrainResult

 1 public IEnumerator SaveTrainResult(TrainResultEntity tre){
 2     if(tre == null){
 3         yield return 0;
 4     }
 5  
 6     string path = DataSourceUtil.SaveTrainResultPath (tre);
 7     if(string.IsNullOrEmpty(path)){
 8         yield return 0;
 9     }
10 
11     WWW www = new WWW (path);
12     yield return www;
13  
14     if (www != null) {
15 
16         if(!string.IsNullOrEmpty(www.error)){
17 
18             Debug.Log("实训考核结果保存失败!" + www.error);
19 
20             www.Dispose();
21 
22             www = null;
23         }else{
24             Debug.Log("实训考核结果保存成功!" + www.text);
25             DatasFromServer.CurrentTrainResult.Id = int.Parse(www.text); 
26         }
27     }
28     www = null;
29     
30 }    

 

 

 

 



评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!