tags:

views:

650

answers:

4

Hi I have a problem how to detect when the network goes offline. My server and client are in different computer. but connected by LAN Actually I have written tcp socket programming in java. Once both server and client are connected then if i write something and put a sleep of 10 sec then I remove my LAN cable.

After 1 min I plugged my LAN cable. But I got that transaction can occur between server and client as usual.

Thanks Sunil Kumar Sahoo

A: 

If all goes as usual, I suggest you don't worry about the computer going offline.

Otherwise, a 'ping' implemented at the application level is in order, i.e. the client sends a simple ping message and expects a response from the server.

Robert Munteanu
A: 

TCP will survive a 2 minute connection loss. Any packets that were lost will be retransmitted. This is by design: The Internet should survive a nuclear war and network hardware also wasn't that reliable when the 'Net was invented.

If you need to know faster, you must send a small test message to the server and handle the timeout (no response within a few seconds) yourself, probably in a different thread.

Aaron Digulla
+2  A: 

If I understand your question correctly, you need to be able to quickly decide whether the network is slow or completely down.

This is not an easy thing to do.

There are two different situations here. Either your source computer, or some router along the path, are aware of permanent problem with the route to your destination. Or there is no such awareness.

For example, if you pull the LAN cable out of your source computer (the one that sends the request and waits for response), the network driver would be aware that the connection top the world is gone. It would notify you of an error instantaneously. This is the first kind of situation.

On the other hand, if you pull the plug from the destination computer, the destination gateway has no knowledge that that computer went down. Your requests will be sent out on the destination subnet, but they will of course be ignored. This is the second kind of situation.

In the case of remote failure (teh second kind of problem), your TCP stack will eventually decide that the destination machine is not talking. However, it takes minutes. It's a system configuration parameter, I believe.

The only solution that I am aware of is to have asynchronous on-the-wire application level protocol, and some sort of heartbeat message, with reasonable timeout (seconds).

Arkadiy
+1  A: 

What I have done in the past is write a small connection monitor class that uses a repeating java Timer. When the timer goes off, I check on the state of the connection by asking the connection for three kinds of information: ifConnected (which verifies if it has connected in the past), !isClosed() (so, the connection should still be active), and isReachable() (can the destination be reached?). Something like this (use as an example, not a solution):

public static boolean isConnected = false;
// pretend other stuff is properly declared in the surrounding class

private class CxMonitor {
   CxMonitor() {
      javax.swing.Timer timer = new Timer(5000, new ActionListener() {

      // what happens when the timer goes off
      public void actionPerformed(ActionEvent e) {
         isConnected = mySocket.isConnected() &&
                      !mySocket.isClosed() && 
                       InetAddress.getByName(severAddress).isReachable(1000);
         }
      }

      timer.setRepeats(true);
      timer.start();
   }
}

That has enabled me to monitor a socket connection status. Then, I perform actions in other parts of my code based on the state of the isConnected boolean.

Dopyiii
Thnanks Dopyiii, Really this logic that you implemented really helped me alot
Hope it gets you started! In the grand design here, I had a singleton with a collection of "connections". That way, the static singleton will stay active for the life of the program. Each "connection" object has a threaded timer like this that updates its state. In the past I've also created a monitor singleton that iterates through the collection of objects and asks for their states. Using that, I could make a determination as to the total state of all socket connections. Make sense?
Dopyiii
isConnected and isClosed won't change unless you call the appropriate methods. They won't change asynchronously. isReachable will check that a host is reachable, but the service itself could be down.
Peter Lawrey