我编写了一个异步 UDP 客户端来与我公司的服务器通信。当我在我的开发机器上运行时一切都很好。当我部署到另一台机器时,第一次通过套接字发送数据时,我在 EndReceive 上遇到套接字异常。我的开发盒是 Win7,我已经部署到 XP SP3 机器和 Server 2003 R2 机器上。下面是接收代码:
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the state object and the client socket
from the asynchronous state object.'
Dim state As StateObj = CType(ar.AsyncState, StateObj)
Dim client As Socket = state.sockArg
' Read data from the remote device.'
Dim bytesRead As Integer
receiveDone.WaitOne(Timeout.Infinite)
bytesRead = client.EndReceive(ar)
If bytesRead > 0 Then
Dim s As String = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
parsedata(s)
End If
Catch SockEx As SocketException
mlog.Error(String.Format("ID={1} {0} SocketError={2}", SockEx.Message, ID.ToString, SockEx.SocketErrorCode.ToString), SockEx)
Catch ox As System.ObjectDisposedException
mlog.Warn(String.Format("Object Disposed ID={0}", ID.ToString))
Catch ex As Exception
mlog.Error(String.Format("{1} ID={0}", ID.ToString, ex.Message), ex)
End Try
End Sub 'ReceiveCallback
我得到的例外是:
System.Net.Sockets.SocketException:由于线程退出或应用程序请求,I/O 操作已中止
在 System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
在 RTSPc.Connection.ReceiveCallback(IAsyncResult ar)
SocketException 被操作中止
它在您的开发机上没有失败的原因很可能是 Vista 中 I/O 系统的底层行为发生了更改,以便线程退出时不再取消线程发出的重叠 I/O 。
请参阅我的博客上关于此的帖子:http://www.lenholgate.com/blog/2008/02/major-vista-overlapped-io-change.html
现在,为什么在 XP 上遇到问题才是真正的问题,要回答这个问题,我们可能需要更多地了解如何发出重叠 I/O 请求以及从何处发出。您是否正在运行自己的任何线程?他们发出任何 I/O 请求吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)