when socket know the peer machin has already been down?

hi,

I have developed a very simple echo client/server application. I expect whenever i shutdown the server machine, the client peer detects the shutdown.In reality this doesn't happen and client continues to send and receive data.To my surprise, it even recv() returns with value greater than zero! I stops to recv() after a long period! ( about 5 minute later. )

Where is the problem? How can i detect the peer's shutdown?

Thanks in advance,
Behzad

It couldn't be receiving data if there weren't data left in the queue to receive. How often do you check?

TCP connections by nature can take several minutes to break. It's a "best-effort" protocol which is very stubborn about waiting and retrying. I once saw a connection survive a 15-minute network outage then come back alive with a torrent of backed-up data. UDP on the other hand either goes or it doesn't.

How can i tell the TCP socket to acknowledge me dropping of the connection and not to try anymore?

To reiterate, TCP takes a while to detect and close broken connections because that is what it is designed to do, because it's a stream socket with best-effort delivery. If you don't want that behavior, don't use a stream socket with best-effort delivery. UDP would be more appropriate. You could also use ICMP -- that is, ping.

in fact i was using TCP for a fault tolerant system(Redundant Servers). I have 2 servers.One is in standby mode and the other in active mode.Whenever the active server went down, the standby server takes the role.I wrote a small application that is run on both servers. In one it is in server mode and in another it is in client.For this scenario it seems that TCP is not well suited. By your suggestion about "best-effort" delivery mechanism i read internal details of TCP protocol and reached your experience! Thank u.
Any way, i will try ping. it may help to reach my goal.Any suggestion for my redundant servers well appreciated.

Thanks in advance
Behzad