在定时任务线程中,使用异步socket作为client, EndConnect成功,但是取得RemoteEndPoint时候发生 SocketException:
Exception: 系统检测到在一个调用中尝试使用指针参数时的无效指针地址。
从Server端看到的是:
10053(WSAECONNABORTED):
无法从传输连接中读取数据: 您的主机中的软件中止了一个已建立的连接。
或者:
无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。
在一个简单程序中试验,同样的代码没有问题。
快烦死了,断断续续查了好几天。 调查过程:
0. 关闭360杀毒,木马,windows 防火墙。 统统没有效果!
1. 打开Network tracing(http://msdn.microsoft.com/en-us/library/a6sbz1dx.aspx). 经过仔细对比,发现BeginConnect()调用后:
在简单验证程序中,没有发生BeginConnecttEx()
在发生问题的程序中,BeginConnect()会导致内部调用BeginConnectEx()
2. google 搜搜BeginConnectEx, 全球没有几个,呵呵. 找到下面文章:
Possible System.NET bug? SocketException when performing Async download in Async thread
3. 以此为线索,找到 :
在 Windows Server 2003、Windows VISTA、Windows XP 中修复损坏的 Winsock2
http://support.microsoft.com/kb/811259
按照其中的提示,Msinfo32 发现winsock2的 部分多了几个iku6的什么东东。 不管了,按照提示修复:
netsh winsock reset
然后重新启动。试试,OK乐。
天哪,花费我太多时间!!!!!!!!!
附:OK后的连接过程:
System.Net.Sockets Verbose: 0 : [5116] Exiting DNS::GetHostAddresses() -> IPAddress[]#58501464
System.Net.Sockets Verbose: 0 : [5116] Socket#8318867::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [5116] Exiting Socket#8318867::Socket()
System.Net.Sockets Verbose: 0 : [5116] Socket#8318867::BeginConnect(1:110#26877122)
System.Net.Sockets Verbose: 0 : [5116] Socket#8318867::BeginConnectEx()
System.Net.Sockets Verbose: 0 : [5116] Socket#8318867::InternalBind(0:0#0)
System.Net.Sockets Verbose: 0 : [5116] Exiting Socket#8318867::InternalBind()
System.Net.Sockets Verbose: 0 : [5116] Exiting Socket#8318867::BeginConnectEx() -> ConnectOverlappedAsyncResult#36573035
System.Net.Sockets Verbose: 0 : [3616] Socket#8318867::EndConnect(ConnectOverlappedAsyncResult#36573035)
System.Net.Sockets Verbose: 0 : [3616] Exiting Socket#8318867::EndConnect()
System.Net.Sockets Verbose: 0 : [6032] Socket#5794280::Send()
System.Net.Sockets Verbose: 0 : [6032] Data from Socket#5794280::Send