我编写了一个异步 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