Click to See Complete Forum and Search --> : Detecting closed connection
matze42
March 17th, 2005, 01:03 AM
Hi,
I'm using standard-winsock(2.0) writing a simple TCP server application.
First i listen to a port, then accept an incomming connection and then
doing some communication.
I know, if the client closes the connection gracefully a final Send with 0-Bytes is done.
Now my question:
Is it possible to detect anyhow, if the client closed the connection NOT gracefully,
for example removing the network-cable, killing his application, or something like this.
Any suggestions are welcome.
Matze
svenhag
March 17th, 2005, 01:58 AM
If the application is killed then then kernel will close all open descriptors and when the socket gets closed, a FIN will be sent to the other side to indicate that the connection has been closed.
There's no way to know if the cable has been disconnected except if you send something and doesn't get an answer at all. Also when the cable is disconnected the connection is not closed. It will remain open on both sides and once you connect the cable again you will be able to continue the communication.
You might receive a RST if the cable is disconnected and the host is restared and then the cable is connected again. The connection will be ESTBLISHED on the other side and when that side retransmits the data a RST will be sent back from the restarted host.
matze42
March 17th, 2005, 02:45 AM
Hi svenhag,
thanks for your reply.
What exactly do i receive when a RST is sent.
I know, that when a FIN is sent, i get 0 Bytes in a "recv" call.
I think the disconnected-cable-case i can live with.
Matze
svenhag
March 17th, 2005, 03:10 AM
A RST will make recv return -1 with errno set to ECONNRESET.
matze42
March 17th, 2005, 03:18 AM
Voila,
thank you very much.
This will work for me.
Matze
svenhag
March 17th, 2005, 03:34 AM
Edit my posts a little since they were 'pre-coffee' posts :P and contained some errors.
matze42
March 17th, 2005, 03:53 AM
Ah see,
don't mind.
It's still OK for me.
So i will go and get a cup of coffee too, to avoid
dangerous pre-coffee effects on my programming :) .
Greetings,
Matze
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.