部署时出现 VB.NET 3.5 SocketException,但开发机器上未出现

发布于 2024-08-08 20:10:27 字数 1471 浏览 3 评论 0原文

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

I have written an async UDP client to talk to a server at my company. When I run on my developer machine all is well. When I deploy to another machine I get a socket exception on EndReceive the first time I send data over the socket. My dev box is Win7 and I have deployed to both an XP SP3 machine and a Server 2003 R2 machine. Below is the receive code:

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

The exception I get is:

System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at RTSPc.Connection.ReceiveCallback(IAsyncResult ar)

The SocketException is OperationAborted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-08-15 20:10:27

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

请参阅我的博客上关于此的帖子: http ://www.lenholgate.com/blog/2008/02/major-vista-overlapped-io-change.html

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

It's likely that the reason that it doesn't fail on your dev box is that the underlying behaviour of the I/O system was changed in Vista so that overlapped I/O that was issued by a thread is no longer cancelled when the thread exits.

See this posting on my blog about this: http://www.lenholgate.com/blog/2008/02/major-vista-overlapped-io-change.html

Now, why you're getting the problem on XP is the real question and to answer that we'd probably need to know a little more about how you're issuing your overlapped I/O requests and from where. Are you running any threads of your own? Do they issue any I/O requests?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文