none
TcpClient - Disconnected from server RRS feed

  • Question

  • Hello,
    I use a synchronous connection.
    Works well, but if the server closed, the property TClient.Connected is true.
    Why?
    What can I do it?
    private TcpClient TClient;
    public void Connect(string ip, int port)
    {
    	if (IP == ip && port == Port && TClient != null)
    	{
    		if (TClient.Connected)
    		{
    			Trace.Debug($"Is connected {ip}, {port}");
    			return;
    		}
    	}
    	
    	or
    	
        if (IP == ip && port == Port && TClient != null)
    	{
    		if (TClient.Connected && TClient.Client.Connected)
    		{
    			Trace.Debug($"Is connected {ip}, {port}");
    			return;
    		}
    	}

    Regards Markus
    Tuesday, April 10, 2018 4:41 PM

Answers

  • Hi Markus Freitag,

    Thank you for posting here.

    For your question, the Connected property only reflects the state of the connection as of the most recent operation. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost.

    We also suggest to use TcpConnectionInformation to check whether it is disconnected or not via TcpState.

    Here is a simple example in StackOverFlow for your reference.

    https://stackoverflow.com/questions/1387459/how-to-check-if-tcpclient-connection-is-closed/19706302

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 11, 2018 6:55 AM
    Moderator
  • The status of a socket is not continually polled by .NET. The connection status is updated only when using the connection. So to know that it is now disconnected you'd have to call something that triggers the usage of the socket.

    This SO post discusses one approach to doing this.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 10, 2018 5:10 PM
    Moderator

All replies

  • The status of a socket is not continually polled by .NET. The connection status is updated only when using the connection. So to know that it is now disconnected you'd have to call something that triggers the usage of the socket.

    This SO post discusses one approach to doing this.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, April 10, 2018 5:10 PM
    Moderator
  • Hi Markus Freitag,

    Thank you for posting here.

    For your question, the Connected property only reflects the state of the connection as of the most recent operation. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost.

    We also suggest to use TcpConnectionInformation to check whether it is disconnected or not via TcpState.

    Here is a simple example in StackOverFlow for your reference.

    https://stackoverflow.com/questions/1387459/how-to-check-if-tcpclient-connection-is-closed/19706302

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 11, 2018 6:55 AM
    Moderator