Search code examples
.nettcpclient

How to close a TcpClient when the other end has been closed in .NET?


I have a class that handles TcpClients. What the class should do is:

    while the other end has not done a graceful close or we are stopping
    {
        receive a request
        process it
        send response
    }

As I don't know when the other client will send a request I can not do a Read with a timeout set, so what I have until now is this:

    While Not Me.Stopping()
        Try
            If tcpClient.Available >= My.Settings.minimumModBusTcpFrameSize Then
                processer = New MessageProcesser(Me, tcpClient)
                processer.ProcessMessage()
            End If
        Catch ex As TimeoutException
            ''#Do not nothing, the current message will timeout on origin too.
        End Try
    End While

The problem with this approach is that I never know when a client has done a remote call to Close().

Is there a way of solving this problem?


Solution

  • The solution is at this question:

    How to read with a TcpClient until the other side closes or we stop in .Net