// JP opened flex table

Click to See Complete Forum and Search --> : WSASend returns zero (success) even when socket connection reset at the other end


ethevan
November 30th, 2007, 05:11 AM
I' m using iocp architecture in my server application. One of my client got disconnected due to network problem. But the WSASend to the particular client socket returns zero. Completion port notifies a write completed operation with the specified number of byes written.

Why is it that the TCP socket disconnection at client side not been detected at server side, and the WSASend at server returned without showing socket error?

Doron Moraz
November 30th, 2007, 05:34 AM
Hi Ethevan,

When your client disconnect without ending the session properly, Your server doesn't know it.
WSASend function sends data on what it thinks is a connected socket and return 0 when the data was sent.

Regards
Doron Moraz

ethevan
November 30th, 2007, 05:41 AM
Thanks for the prompt reply. My doubt is, how can we get the write completed notification on a tcp socket which is reset from the other side?

Is there any other way, by which server can identify the connection loss? Since this is net disconnection, client will not be closing it properly. Server will keep on sending the data and will assume that the client receives all the data, but infact the data gets lost.

Doron Moraz
November 30th, 2007, 05:51 AM
Take a look at:
http://www.codeguru.com/forum/archive/index.php/t-363683.html

Regards
Doron Moraz

MikeAThon
November 30th, 2007, 11:51 AM
Yes, as indicated in the thread cited by Doron, unless the remote side does a graceful shutdown, the only way to determine that the remote side is disconnected is by calling recv() ( or WSARecv() ) and detecting a returned value of zero bytes read.

Mike

ethevan
November 30th, 2007, 12:03 PM
Thank you Doron Moraz and Mike.

I would try that option. But one more doubt, why is it that, WSARecv() returns error, while WSASend completes successfully, and the completion port gives a notification too for successful write completion with the specified number of bytes?

regards,
Ethevan

Doron Moraz
November 30th, 2007, 01:49 PM
Hi Guys,

Allow me to suggest more techniques to detect if a client didn't disconnect gracefully.

1. Implement a request/response mechanism.
This will allow you to send a request to your client and expect a response back. You can assume that the connection is down or faulty if it doesn't append.

2. Let your client scream I'm alive.
All you need to do is to send a packet periodically with a signature your server will recognize and acknowledge back to the client.
If this packet will not arrive after a certain amount of time, make the assumption.

If you are thinking ICMP pings. Stop that thought now.
Don't use unreliable protocol to build a reliable check.

3. If you don't mind the overhead and you are using windows 2000 or higher, you can also use Winsock keep alive option.

One more thing, you must understand that different networks behave differently.
If you were using PPP I doubt it if this problem will ever occur.
In contrast Ethernet doesn't establish a link level connection. So we must do it ourselves.

Regards
Doron Moraz

//JP added flex table