Click here to Skip to main content
15,894,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having some trouble with the "socket.Receive()" method in C#. It is a blocking method that waits to receive data, but in my testings, when the socket is connected and it's receiving (blocked) if the connection goes down (like, unplugging the cable) this method will be still "connected" to...well, nothing i guess! It does not cause an exception or anything else...

Why, how does this happens?
Posted

That is correct. Due to the nature of networking and the protocols you use, such disruption can not be detected immediately. If you wait some more, you well get an exception. If you need faster response on network disruption, create some custom mechanism of network testing, base, for example of pinging with the timeout you prefer. See the class System.Net.NetworkInformation.Ping, http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx[^].

Remember, it would be optimistic mechanism, so you need to take care about possible false positives; so your code should be able to handle it.

All you do with networking involved threading, even if you do basic stuff.
Please see my past Answers on the networking threaded design:
Multple clients from same port Number[^].

This is my collection of references to my past Answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^].

—SA
 
Share this answer
 
v2
Comments
nuno_god 12-Apr-11 12:45pm    
SA, regarding that is a single application using this blocking method, do you think that to use System.Net.NetworkInformation.Ping I should have threads? I have implemented the KeepAlive property also, and also modify the registry value of KeepAliveTime and KeepAliveInterval, but in this case do I really need it?
Sergey Alexandrovich Kryukov 12-Apr-11 13:20pm    
Please see my updated Answer.
Not only you need threads -- you should have used threads in the very beginning. Stop even playing with the idea to do anything at all not in the threaded way!
Please see my references -- my past Answers give a pretty good idea on what's involved.
I don't thing you should ever touch the registry. Do it on application level. Have some configuration where you can play with parameters.
--SA
Sergey Alexandrovich Kryukov 12-Apr-11 13:20pm    
And thanks for accepting this Answer.
--SA
Espen Harlinn 12-Apr-11 15:25pm    
Nice answer, my 5
Sergey Alexandrovich Kryukov 12-Apr-11 16:23pm    
Thank you, Espen.
--SA
[Adding to SA's as usual excellent response]

Here are some alternate suggestions to this problem in a S.O. thread:

http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket[^]
 
Share this answer
 
Comments
Espen Harlinn 12-Apr-11 15:26pm    
Good points :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900