您的位置:首页 > 理论基础 > 计算机网络

解决C#网络通信编程的阻塞问题

2013-01-14 15:48 190 查看
  网络编程分为阻塞和非阻塞两种方式,如果选择了阻塞方式进行编程,控制不好很容易导致程序死掉。

  下面这段代码中:

...
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在读不到数时就永远不会返回。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: