Click here to Skip to main content
15,615,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a chat application. When a user disconnects(with end proccess of app, disconnecting from network ,...) the Socket in the server is Connected=True. How i cant understand when a user disconnects without saying to server?(like yahoo messenger. when a user disconects other friends will see).

1 solution

Socket.Connected represents the state of the socket at the last read or write operation, not the current state of the socket. There are several different ways to handle disconnects:

1) When the client disconnects it can send a message to the server saying that it's about to disconnect.
2) Have the server use the Socket.Poll[^] method to get the current status. The return value from that can be combined with other information to determine if the socket is still open.
3) Have the server periodically send out a heartbeat message to the clients to test the connection. If it's closed then the send will fail.

Using (1) will give you instantaneous notification when a client drops and (3) will handle clients that improperly drop their connections. (3) will also keep the client aware of the socket state since it can check for the heartbeats. (2) is similar to (3) except that nothing is sent to the clients so both client and server would have to actively check the socket state.
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