views:

372

answers:

4

Hi all, I want to check when the internet goes off can i capture that event .I am not getting the proper API or any example which would explain the same .

I am using socket for (TCP)communication and I open a socket when the network is available. I have observed that the socket does not give any exception in case the network goes off.

If any one had done or any example links would be really helpful Thanks in advance

+2  A: 

It's not that easy to tell whether the network is off or just slow.

If you set Timeouts, it will throw exception if it takes too long:

For sockets:

socket.setSoTimeout(CONNECTION_TIMEOUT);

For HttpURLConnections:

HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setConnectTimeout(CONNECTION_TIMEOUT);
con.setReadTimeout(CONNECTION_TIMEOUT);
Jerome
Hi Jerome. thanks for answering. My Problem is my socket is opened successfully when i was network was available. Now If network goes off, I am looking for some event which can tell me that network is down. (I am using TCP connection)
Nilesh
From the javadoc of setSoTimeout:Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
Jerome
The SocketTimeoutException would be your event telling you that the network is down. It is not raised on the creation of the socket, but when you try en read from its InputStream
Jerome
Jerome, I am not having any problem if the data is coming slow. I want to detect the disconnection of network(internet) in my machine.
Nilesh
Hum, okay... I'm not sure that's even possible with TCP. What's the difference between a huge lag (a router somewhere between you and the server going crazy) and a disconnection?
Jerome
No difference if peak between a round-trip packet is greater than timeout :D TCP cannot distinguish them. It will consider big lag as an actual disconnection.
Jack
That was a rhetorical question ;)
Jerome
+2  A: 

The problem is that no event 'network down' exists in tcp connections, they just go down.

As suggested by Jerome you should check if timeout is reached.

Of course if network goes down you won't receive packets neither be able to send them so the underlying InputStream and OutputStream will throw an IOException but just when they'll realize that network is not working properly (usually 2*rtt = 120 seconds, it depends how TCP layer is managed).

Look state diagram by yourself: alt text

What typically happens is that when in ESTABLISHED your socket will send data over the socket while waiting for ACK from destination. ACK won't come since network went off so your socket's window fills up and socket starts resending packets until real timeout intervenes throwing the exception.

Another case is when network goes off and your socket realizes that it cannot write anymore on channel: it will throw an exception imediately upon calling outStream.write(...).

Jack
Thanks for answering Jack. As TCP communication is based on socket and TCP is quite reliable, if the network disrupts between connected sockets , shouldn't socket get NULL or throw some exception??
Nilesh
No, the packet will be resent later on.
Jerome
Hi Jack. Thanks for the elaborated explanation. Its so surprising that the in mobile developement, such events are provided (for e.g. androide, windows mobile(C#), iphone(objective c)) but java being such huge platform doesn't provide any event for such purpose?
Nilesh
If i disable my Network , no OS level event will be fired, which i can catch and handle? If I can accomplish that my problem will be solved.
Nilesh
The problem is not java, it is TCP/IP. On your mobile, you can easely tell if you can communicate with a cell, because data is exchanged frequently and with a very low timeout.
Jerome
Ok. So that is the reason why it is easy to do in Mobile.
Nilesh
A: 

Why don't you try pinging www.google.com

See http://java.sun.com/j2se/1.5.0/docs/guide/nio/example/Ping.java

crowne
+2  A: 

TCP is designed to be quiet when idle. There is no administrative packets on wire when there is no pending packet. If the connection is dead while idle, you will not know, no matter what the setting of the timeout is. It does have keepalives but it's pretty much useless at the recommended frequency of 2 hours and longer.

You need to build some heartbeat or keepalive in your application protocol to detect stale connections. Keepalive is nothing but a noop packet sent at regular interval to trigger TCP timeout when connection is down. In my app, I do this every 10 seconds.

ZZ Coder
Hi ZZCoder. Thank you so much for replying. Can you give me the demo code of your implementation for the better idea.
Nilesh
The protocol is proprietary and I can't share it with you. It can be really simple. You must have some application frames to exchange data. Just send a special NO-OP frame or regular frame with no data, once a while using a timer. In public protocols, PING-PONG in IRC is a good example: http://irchelp.org/irchelp/rfc/chapter4.html#c4_6_2
ZZ Coder