解決C#網絡通信編程的阻塞問題

  網絡編程分爲阻塞和非阻塞兩種方式,如果選擇了阻塞方式進行編程,控制不好很容易導致程序死掉。

  下面這段代碼中:

複製代碼

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在讀不到數時就永遠不會返回。

發佈了19 篇原創文章 · 獲贊 9 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章