recv won't return on socket close  
Author Message
goldsz





PostPosted: Visual C++ Language, recv won't return on socket close Top

Sorry to make my first post a cry for help but I'm running out of places to look.

I have a TCP server that accepts connections and than handles the new client sock in a new thread. Everything is working fine except when the client closes ungracefully the socket stays open and my recv is still blocking instead of returning SOCKET_ERROR. I've shutdown the remote client box but my recv won't return and netstat still shows the TCP socket to be ESTABLISHED. It's not a huge deal but I'm leaving threads laying around and not realeasing my socket handles.

Any ideas

Thanks.



Visual C++12  
 
 
Martin-W





PostPosted: Visual C++ Language, recv won't return on socket close Top

Hi,

my understanding of TCP is, that it tries to handle network problems gracefully. E.g. if you have a telnet connection to some other computer and unplug the network cable not on one of the computers, but e.g. on some intermediate link, then the connection stays alive and telnet will continue to work after you plug the cable in again. Unplugging the cable directly on one of the computers may automatically switch the network interface to "down" and inform the application about that, but if -- as you said -- the client closes ungracefully, then no notification is sent to the server and the server still waits for the client. There is no means for the server to distinguish this case just from "a very bad network connection".

AFAIK there are some timeouts to detect this situation, but these are more in the range of several minutes instead of seconds. If you need faster reaction, then you could try to tweak this timer (don't ask me how!), however this may affect all applications on your server! Another possibility could be to implement some time of keep-alive protocol on the socket and if the answer does not come back within some time, then the server may close the socket.



 
 
goldsz





PostPosted: Visual C++ Language, recv won't return on socket close Top

Martin,

Unfortunatly I can't change the client/server interface so I can't implement a heartbeat. Timeout isn't an option because the client can stay connected for any period of time before/between sending data blocks. I thought TCP implemented its own heartbeat message so that it would know when the opposite side went down. I guess I won't ask you about configuring TCP timeout but I'll look into it.

Thanks.


 
 
pintu*





PostPosted: Visual C++ Language, recv won't return on socket close Top

Only One THing you have to Consider .Recv is a Blocking call so at the time when your server will stop responding the recv function goes in a infinite loop.i think due to this condition only you are facing problem .Have a look in your code.if still problem please let us know

Thanx


 
 
einaros





PostPosted: Visual C++ Language, recv won't return on socket close Top

Please direct networking issues to http://msdn.microsoft.com/newsgroups. This is off topic for this forum. See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=789657&SiteID=1 for more information.