解决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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章