开发者

How to properly handle disrupted TCP connection?

开发者 https://www.devze.com 2023-01-31 13:57 出处:网络
I\'m using TCP socket connetion between a server program and a client program. Multiple client programs shall connect to the same port. The problem is that if I close a client program I get the follow

I'm using TCP socket connetion between a server program and a client program. Multiple client programs shall connect to the same port. The problem is that if I close a client program I get the following error on the server-program:

System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)

http://i55.tinypic.com/nh135w.png

If I handle this by a try/catch, I am then not able to re-connect with the client-program, as it gives the following error (in the client program):

No connection could be made because the target machine actively refused it 127.0.0.1: 1234

Below is the listener code in the server program. I hope to get some help to understand how I can handle client program shutdown/restart&reconnect without the server program failing..

   ' This is the callback function for TcpClient.GetStream.Begin, It begins an asynchronous read from a stream.
    Private Sub DoRead(ByVal aread As IAsyncResult)
        Dim BytesRead As Integer
        Dim strMessage As String

        ' Try
        ' Ensure that no other threads try to use the stream at the same time.
        Sy开发者_如何学编程ncLock _client.GetStream
            ' Finish asynchronous read into readBuffer and get number of bytes read.
            If Client.Connected Then
                BytesRead = _client.GetStream.EndRead(aread)
            End If
        End SyncLock

        ' Convert the byte array the message was saved into, minus one for the Chr(13) (carriage return).
        strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 1)
        ProcessIncoming(strMessage)

        ' Ensure that no other threads try to use the stream at the same time.

        SyncLock Client.GetStream
            ' Start a new asynchronous read into readBuffer.
            Client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
        End SyncLock
        'Catch e As Exception
        '    ' This triggers if userlist is found empty
        '    ' Then gives problem, as it cant close the connection or something.. ??
        '    Debug.Print("UserConnection.DoRead Exception: " & e.ToString)
        '    CloseConnetion("Error: Stream Reciever Exception")

        'End Try

    End Sub


You don't. You're the server. You close the socket and forget about it. If the client wants more service it is up to him to reconnect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号