我写了一个异步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:由于在RTSPc.Connection.ReceiveCallback(IAsyncResult ar)处的System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)处的线程退出或应用程序请求,I/O操作已中止.
SocketException是OperationAborted
它可能是你的开发盒没有失败的原因是在Vista中改变了I/O系统的基本行为,因此当线程退出时,线程发出的重叠I/O不再被取消.
请在我的博客上查看此帖子:http://www.lenholgate.com/blog/2008/02/major-vista-overlapped-io-change.html
现在,为什么你在XP上遇到问题是一个真正的问题,并且回答我们可能需要更多地了解你如何发布重叠的I/O请求以及从何处发出.你在运行自己的线程吗?他们发出任何I/O请求吗?