How to know when connection dropped?

Started by
4 comments, last by Hoser 20 years, 6 months ago
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.
Advertisement
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.
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]
When using the Windows calculator program, always remember to clear any values from memory before exiting to prevent burn-in.
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.
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
Uhhh...
OT: it''s hilarious that your nickname is Hoser and you''re from Canada... LOL

This topic is closed to new replies.

Advertisement