去你爷爷的MSDN!

本周客户那边出现了一个很严重且紧急的BUG,大致情况是,当通过.Net Framework 1.1的TcpClient连续高频率访问某个Socket服务器的时候,第三次或之后的连接请求就会出现超时错误(10060),因为一些特殊原因,不方便在该服务器上调试,只好靠猜测、修正、验证、猜测、修正、验证……

原因终于是调查清楚了,Socket客户端、服务器端的原因都有,但是主要原因还是在客户端,发现原来是开发人员受了MSDN的误导,在完成一次TCP访问之后,调用TcpClient.Close之前,没有显式地调用NetworkStream.Close,因为服务器端在某个时间范围内不会主动关闭该TCP连接,并且线程数和最大连接数有限,所以导致后来的连接请求发生超时错误。

开发人员不显式调用NetworkStream.Close的理由是,根据MSDN的以下记述:

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.close(vs.71).aspx

.NET Framework Class Library 1.1
TcpClient.Close Method
Closes the TCP connection and releases all resources associated with the TcpClient.

[C#]
public void Close();

Remarks
The Close method closes the TCP connection. It calls the Dispose method passing a true value to release all managed and unmanaged resources associated with the TcpClient. These resources include the underlying Socket used for connecting with the remote host, and the NetworkStream used to send and receive data.

既然调用TcpClient.Close之后该方法里面的处理会自行释放NetworkStream等一切相关的托管/非托管资源,那么就没必要再显式地去释放这些资源,否则便有叠床架屋之嫌,而且会增加无意义的代码,引入不必要的复杂性。

但是,实际上发现,MSDN这么说,而MS的代码实现时却没有这么做!带着困惑和焦虑摸索了将近一个星期之后,我们找到了以下这个KB:

http://support.microsoft.com/kb/821625/en

SYMPTOMS
When you call the TcpClient Close method, you expect the underlying TCP connection to be closed as described in the "TcpClient.Close Method" topic of the .NET Framework Class Library documentation.

RESOLUTION
To resolve this problem, close the associated NetworkStream object that the TcpC...To resolve this problem, close the associated NetworkStream object that the TcpClient GetStream method returns.

根据这个KB修正了客户端的代码,问题自然是解决了,但是我还是忍不住骂一句:去你爷爷的MS!去你爷爷的MSDN!为什么,因为对于任何一个使用微软技术的开发人员来说,MSDN就等于Bible,我们宁愿先怀疑自己,也不会先去怀疑MSDN,微软有义务保证MSDN的正确性和权威性。

后来我们查阅了.Net Framework 2.0和3.0的API说明资料,发现同一方法的Remarks部分已经做了修正(或仅仅是2.0以后独有的特征?只能猜测)。

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.close(VS.80).aspx

.NET Framework Class Library
TcpClient.Close Method
Disposes this TcpClient instance without closing the underlying connection.

C#
public void Close ()

Remarks
The Close method marks the instance as disposed but does not close the TCP connection. Calling this method does not release the NetworkStream that is used to send and receive data. You must call the Close method to close the stream and the TCP connection.

其实因为微软的文档或程序BUG已经导致我们发生过很多不愉快事件了,这次简直失望得无以复加,作为一个主要面向微软技术的技术工作者,心寒呐。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值