Thursday, January 20, 2005

Detecting Disconnected Network Cable through Java Sockets

It would be my guess that there seems to be a misunderstanding of what sockets do and what the phyiscal layer (the cable) has to do with it. First a socket is a virtual connection. The cable has nothing to do with it. There is no way that a socket knows that a cable has been pulled. If you need that information then you are going to have to use something besides a socket. A socket however can tell you if a communication has failed. But that only happens when you send something. Reading something will not do it. You might expect to receive something on a socket in a given time period and thus a read "fails." But that has nothing to do with the socket itself. You have to write code to do that yourself (and that is a limitation of the socket not java.) When a socket sends something it uses UDP or TCP (at least in java.) With UDP it just goes. There is never a failure even if it doesn't go anywhere. With TCP the TCP protocol (software, nothing to do with hardware like cables) waits for a receipt response for a fixed length of time. If it doesn't receive the receipt it sends the message again. It keeps doing this several times until it finally gives up. The only change you can make to this is how many times it tries and how long it waits. This is often a operating system wide change. So if you reduce it you better make real sure that you don't expect traffic with another device. Especially a internet device. Reference : http://forum.java.sun.com/thread.jspa?threadID=316842&messageID=1604338

No comments: