Using the TCPListener you get a TCPClient object to communicate with the 'Client'.
If you use the TCPClient.GetStream.BeginRead method to read the data from the 'Client', then when the 'Client' closes the connection, the CallBack from the BeginRead will contain no data (0 Bytes).
yeah you're partially correct but client can still be connected even if it is not sending any data to the listener. is there other way of knowing if client is still connected or not?
Well, from what I know and use, the client is connected until the server side of the connection receives a 0 lengthed data, which indicates the point when the connection is closed.
From what I can tell there will always be a case where the connection could still be open. You can easily detect just about everything except a dropped connection - ie client pulled the plug, or a mid-point station failed, etc...
The only way to account for every thing is to implement a Ping-Pong system - or basically make sure there is a regular timed transmission between client and server (server initiates, client responds). If the client doesn't respond in a timely manner the connection is considered closed.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.