Click here to Skip to main content
16,020,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two separate applications Client and Server used to send and receive data from Ethernet Cable. The Client application sends the data to server. First when the server application is started, it starts accepting client request to accept data. As soon as client is connected , Server thread is started to receive data from Client. Whenever Client application is stopped, there must be either some command send to the server to notify that it has disconnected or there must be some way to detect client disconnection at server end so that thread can be closed for receiving data from Client. I am using TCPClient for this purpose. Kindly suggest some way to do this.
Thanks

What I have tried:

i am trying to disconnect a client from a server but the server still sees it as being connected.
Posted
Updated 8-Jun-18 8:02am
Comments
Rob Philpott 8-Jun-18 6:18am    
OK, this is not an authorative answer as I'm not absolutely sure, but I think that if you have a TcpClient server-side, and presumably you have got a stream from it from which you are reading what the client sends you.

Reading from the stream synchronously will block, until bytes are received. In the event of a disconnect the call will unblock, but you will have read no bytes.

I *think* you can use this after which you should do the close/shutdown cycle.

Each side of a connection should inform the other side when it closes the connection.

This must be done at data protocol level. For example one side sends a request to stop communication and the other answers with an acknowledge response. The side sending the request closes the connection when the response has been received and the other side after the response has been send.

To handle unexpected disconnections use the function provided in solution 1 by Richard. But note that that is returning the state of the most recent operation which is usually not the actual state. To trigger getting the actual state just send a request to the other side. This might be a special request that must not be answered (kinf of NOOP / No Operation or keep alive). If the other side is no longer present, sending the request will fail and the connection (and the thread) can be closed.
 
Share this answer
 
The "server" "drops" the connection after a period of time if no activity ("times out").

The "server" cannot depend on a client to close "its" connection, because the client(s) could "die" at any given time before having sent any "last requests".
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900