views:

1313

answers:

7

When I'm using e.g. PuTTY and my connection gets lost (or when I do a manual "ipconfig /release" on Windows), it responds directly and notifies my connection was lost.

I want to create a Java program which monitors my internet connection (to some reliable server), to log the date/times when my internet fails.

I tried use Socket.isConnected() but that will just forever return "true". How can I do this in java?

+5  A: 

Well, the best way to tell if your connection is interrupted is to try to read/write from the socket. If the operation fails, then you have lost your connection sometime.

So, all you need to do is to try reading at some interval, and if the read fails try reconnecting.

The important events for you will be when a read fails - you lost connection, and when a new socket is connected - you regained connection.

That way you can keep track of up time and down time.

jjnguy
+5  A: 

Why not use the isReachable() method of the java.net.InetAddress class?

How this works is JVM implementation specific but:

A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.

If you want to keep a connection open continually so you can see when that fails you could connect to server running the ECHO protocol yourself rather than having isReachable() do it for you and read and write data and wait for it to fail.

Dave Webb
Hmm maybe I wasn't clear in my question. What sometimes happens at my systen is that my internet drops for a few seconds and all connections get closed. A second later everything is fine.I want to monitor this on a daily basis, to make reports how often this is happening.
kresjer
You could connect to an ECHO server yourself and keep sending and reading data; added this to the answer.
Dave Webb
A: 

You could ping a machine every number of seconds, and this would be pretty accurate. Be careful that you don't DOS it.

Another alternative would be run a small server on a remote machine and keep a connection to it.

Geo
A: 

Its probably simpler to connect to yahoo/google or somewhere like this.

    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    int dataLen = yc.getContentLength() ;

Neil

Neil Wightman
A: 

You might want to try looking at the socket timeout interval. With a short timeout (I believe the default is 'infinite timeout') then you might be able to trap an exception or something when the host becomes unreachable.

rascher
This does not detect all the cases. You have to read or write something to the socket.
tputkonen
+2  A: 

Even though TCP/IP is "connection oriented" protocol, normally no data is sent over an idle connection. You can have a socket open for a year without a single bit sent over it by the IP stack. In order to notice that a connection is lost, you have to send some data on the application level.(*) You can try this out by unplugging the phone cable from your ADSL modem. All connections in your PC should stay up, unless the applications have some kind of application level keepalive mechanism.

So the only way to notice lost connection is to open TCP connection to some server and read some data from it. Maybe the most simple way could be to connect to some FTP server and fetch a small file - or directory listing - once in a while. I have never seen a generic server which was really meant to be used for this case, and owners of the FTP server may not like clients doing this.

(*) There is also a mechanism called TCP keepalive but in many OS's you have to activate it for all applications, and it is not really practical to use if you want to notice loss of connection quickly

tputkonen
+1  A: 

Okay so I finally got it working with

try
{
    Socket s = new Socket("stackoverflow.com",80);
    DataOutputStream os = new DataOutputStream(s.getOutputStream());
    DataInputStream is = new DataInputStream(s.getInputStream());
    while (true)
    {
        os.writeBytes("GET /index.html HTTP/1.0\n\n");
        is.available();
        Thread.sleep(1000);
 }
}
catch (IOException e)
{
    System.out.println("connection probably lost");
 e.printStackTrace();
}

Not as clean as I hoped but it's not working if I leave out the os.writeBytes().

kresjer
This is something that you definitely should not use. It can almost be considered as a DOS attack against stackoverflow.com!
tputkonen