Archived

This topic is now archived and is closed to further replies.

Hoser

How to know when connection dropped?

Recommended Posts

I''m fairly new to Winsock programming, so please bear with me. I''m writing a small client/server application. The client and the server are both written using MFC CAsyncSocket class. I can run both applications, and they connect fine (SOCK_STREAM), and transmit data properly. However, if I abruptly shut down the server application, the client application still thinks it is connected. The only notification I receive of the connection having been reset is when the client attempts to send some more data to the server. So, my question is this, is there a way for my client application to tell if its connection to the server has failed *without* having to send data? I thought maybe enabling the SO_KEEPALIVE option would do this (I thought that this is what it was for), but that didn''t do it. Also, just for information sake, if the SO_KEEPALIVE option isn''t for this, what is it for? Thanks for any help.

Share this post


Link to post
Share on other sites
Two methods:

1. Server sends out "heartbeat": a special packet every n seconds. If a client doesn''t receive heartbeat after more than n seconds (plus some grace period for variable network latency) it assumes the connection is dropped. If data is always sent on regular intervals anyway, this can be used instead of the heartbeat.

2. If client does not receive any packets from the server within n seconds, it sends an AYT (Are You There) packet, which the server must immediately respond to. If the client does not receive the acknowledgement to the AYT within a short period of time, it assumes the connection has dropped.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Doesn't OnClose() get called in CAsyncSocket when the server shuts down?
What does your Create() call look like when you create the client socket?
If I create a client socket using the default values for all parameters of CAsyncSocket::Create(), CAsyncSocket::OnClose() is called no matter how the server is terminated in all of the cases I've tested.

Shawn


[edited by - ShawnO on October 6, 2003 4:16:31 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ShawnO
If I create a client socket using the default values for all parameters of CAsyncSocket::Create(), CAsyncSocket::OnClose() is called no matter how the server is terminated in all of the cases I''ve tested.

Shawn

That''s because you only tested cases where the OS was allowed to gracefully close the network socket. Try yanking the ethernet cable out of the server-- the connection won''t be terminated.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
By the way, if your app runs on Win32 timers running a tick where any work is done in intervals, increase the priority of your app, so if any other apps are fighting for processor time, doing a large disk load or something, your app won''t be disconnected when the (low priority) timer messages are not recieved for a quanta.
Thanks to whoever on the forum gave me this advice a little while ago. Works well.
Chris

Share this post


Link to post
Share on other sites