2

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

4

1 回答 1

5

它在您的开发盒上没有失败的原因很可能是 I/O 系统的底层行为在 Vista 中发生了更改,因此当线程退出时,线程发出的重叠 I/O 不再被取消.

在我的博客上看到这个帖子:http: //www.lenholgate.com/blog/2008/02/major-vista-overlapped-io-change.html

现在,为什么您在 XP 上遇到问题是真正的问题,并且要回答我们可能需要更多地了解您如何发出重叠的 I/O 请求以及从何处发出。您是否正在运行自己的任何线程?他们是否发出任何 I/O 请求?

于 2009-10-22T20:59:48.740 回答