如何处理TCPListener“远程主机强行关闭现有连接”

时间:2011-08-05 15:41:36

标签: vb.net tcp tcplistener

我有一个使用TCPListener的tcp服务器和异步方法BeginAcceptTCPClient:

 Imports System.Net.Sockets
Imports System.Threading
Imports System.Net

Public Class TCPServer
    Private mPort As Integer
    Public Event IncomingMessage(ByVal Message As String, ByVal IP As String)
    'This signals threadpool threads to stop...
    Private mStopServer As ManualResetEvent
    Private mListener As TcpListener
    Public Sub New(ByVal Port As Integer)
        mPort = Port
        Start()

    End Sub

    Public Sub Start()
        Try
            If mListener Is Nothing Then
                mListener = New TcpListener(IPAddress.Any, mPort)
            End If

            mListener.Start()
            AcceptClients()
            mStopServer = New ManualResetEvent(False)
        Catch ex As Exception
            ExceptionHandling.LogError(ex)
        End Try

    End Sub

    Private Sub AcceptClients()
        Try
            Dim result As IAsyncResult = mListener.BeginAcceptTcpClient(AddressOf HandleAsyncConnection, mListener)
        Catch ex As Exception
            ExceptionHandling.LogError(ex)
        End Try

    End Sub

    Dim so As New Object
    Public Sub StopListening()
        Try
            mStopServer.Set()
            mListener.Stop()
        Catch ex As Exception
            ExceptionHandling.LogError(ex)
        End Try

    End Sub


    Private Sub HandleAsyncConnection(ByVal result As IAsyncResult)
        Try


            If Not mStopServer.WaitOne(0) Then
                Dim listener As TcpListener = DirectCast(result.AsyncState, TcpListener)
                If listener Is Nothing Then Exit Sub
                Dim client As TcpClient = listener.EndAcceptTcpClient(result)
                Trace.WriteLine("Connected to new client")
                ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ProcessClient), client)
                AcceptClients()

            End If
        Catch ex As Exception
            ExceptionHandling.LogError(ex)

        End Try
    End Sub



    Private Sub ProcessClient(ByVal client As Object)

        Dim newClient As TcpClient = DirectCast(client, TcpClient)


        Try
            ' Buffer for reading data
            Dim bytes As Byte() = New Byte(32 * 1024) {}
            Dim clientData As New System.Text.StringBuilder()


            Using ns As NetworkStream = newClient.GetStream()
                ' set initial read timeout to 1 minute to allow for connection
                ns.ReadTimeout = 60000
                ' Loop to receive all the data sent by the client.
                Dim bytesRead As Integer = 0
                Do
                    ' read the data
                    Try
                        If mStopServer.WaitOne(0) Then Exit Sub

                        bytesRead = ns.Read(bytes, 0, bytes.Length)
                        If bytesRead > 0 Then
                            clientData.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, bytesRead))
                            ' decrease read timeout to 1 second now that data is coming in
                            ns.ReadTimeout = 1000
                        End If

                    Catch ioe As IO.IOException
                        Trace.WriteLine(ioe.ToString)
                        bytesRead = 0
                        Dim bError() As Byte = Error400()
                        ns.Write(bError, 0, bError.Length)

                    End Try

                Loop While ns.DataAvailable
                ForwardData(clientData.ToString, newClient.Client.RemoteEndPoint.ToString)
                'Trace.Write(clientData.ToString())
                'Acknowledge success
                bytes = Ack200()
                ns.Write(bytes, 0, bytes.Length)
            End Using

        Catch ex As Exception
            ExceptionHandling.LogError(ex)

        Finally
            ' stop talking to client
            If newClient IsNot Nothing Then
                newClient.Close()
            End If
        End Try
    End Sub

    Private Sub ForwardData(ByVal Data As String, ByVal IP As String)
        RaiseEvent IncomingMessage(Data, IP)
    End Sub
    Public Function Ack200() As Byte()
        Return System.Text.Encoding.UTF8.GetBytes("Okay")
    End Function

    Public Function Error400() As Byte()
        Return System.Text.Encoding.UTF8.GetBytes("Error")
    End Function

End Class

我的问题是很少,我在HandleAsyncConnection方法中得到一个异常: “在EndAcceptTCPClient方法中,”远程主机强制关闭现有连接“。此时,TCPListener似乎停止了监听。问题是我无法轻松测试,因为它只发生在远程测试VM上,并且每24小时左右才会发生一次。如果我知道如何使用测试客户端重新创建错误,我将能够解决这个问题。 Wireshark显示在异常时发送的重置[RST]数据包。所以我要么知道如何处理异常,要么如何使用测试客户端重新创建问题。

1 个答案:

答案 0 :(得分:0)

您可以通过重新启动服务器或弹回与之连接的服务来重新创建问题。基本上任何杀死你正在与之通信的进程的东西都会产生这个错误。

要处理,你几乎必须捕获异常并尝试重新连接到服务器,可能不得不等到它再次可用。

编辑: 您的代码停止侦听,因为您在发生异常的情况下调用AcceptClients,因此您永远不会回到BeginAcceptTCPClient函数。另外,我不确定它如何与Async调用一起工作,但它看起来像是对我的无限递归。 BeginAcceptTCP客户端调用HandlAsynConnection的委托,该委托调用AcceptClients,启动循环。在我看来,在这么多连接之后你应该得到一个堆栈溢出错误。您可以在HandleAsyncConnection中的try块中添加finally子句并在那里调用它,因此在发生错误时始终会调用它。

    Try


        If Not mStopServer.WaitOne(0) Then
            Dim listener As TcpListener = DirectCast(result.AsyncState, TcpListener)
            If listener Is Nothing Then Exit Sub
            Dim client As TcpClient = listener.EndAcceptTcpClient(result)
            Trace.WriteLine("Connected to new client")
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ProcessClient), client)

        End If
    Catch ex As Exception
        ExceptionHandling.LogError(ex)
    Finally            
        AcceptClients()
    End Try