網絡編程分為阻塞和非阻塞兩種方式,如果選擇了阻塞方式進行編程,控制不好很容易導致程序死掉。
下面這段代碼中:
... TcpListener tcpListener = null; TcpClient tcpClient = null; NetworkStream networkStream = null; ... /// <summary> /// 接收線程 /// </summary> private void receive() { while (true) { Thread.Sleep(TimeSpan.FromSeconds(1)); this.AddTextToCommStateList(DateTime.Now.ToString() + "\t等待連接..."); tcpClient = tcpListener.AcceptTcpClient(); Thread.Sleep(TimeSpan.FromSeconds(1)); this.AddTextToCommStateList(DateTime.Now.ToString() + "\t已連接"); networkStream = tcpClient.GetStream(); // 設置讀取超時時間為10秒 networkStream.ReadTimeout = 10000; // 設置寫入超時時間為10秒 networkStream.WriteTimeout = 10000; byte[] recvBuff = new byte[8192]; int readBytes = 0; while (tcpClient != null) { try { this.AddTextToCommStateList(DateTime.Now.ToString() + "\t開始讀取"); readBytes = networkStream.Read(recvBuff, 0, recvBuff.Length); this.AddTextToCommStateList(DateTime.Now.ToString() + "\t讀取完畢"); } catch (IOException ex) { this.AddTextToCommStateList(DateTime.Now.ToString() + "\t接收錯誤"); this.AddTextToCommStateList(DateTime.Now.ToString() + "\t錯誤信息:" + ex.Message); break; } ...
如果去掉networkStream.ReadTimeout = 10000;和networkStream.WriteTimeout = 10000;兩行,程序就有可能在
readBytes = networkStream.Read(recvBuff, 0, recvBuff.Length);處死掉(筆者在調試到此時將網線拔掉,發現程序就會死在這里,即使網絡恢復了程序也不會恢復)。
查看MSDN中關於NetworkStream類的幫助信息,發現ReadTimeout和WriteTimeout屬性的默認值都是Infinite(無限),即永不超時,所以networkStream.Read在讀不到數時就永遠不會返回。