0

Possible Duplicate:
How to check if internet connection is present in java?

I have a java desktop app which connects to a online document. Now how can I determine whether the user is connected to internet or not.

Community
  • 1
  • 1
aamadmi
  • 2,680
  • 2
  • 18
  • 21
  • 2
    Look at this: http://stackoverflow.com/questions/1139547/detect-internet-connection-using-java – Marcos Dec 18 '11 at 17:37
  • 1
    Do you really want to know if the user is connected to internet or you just want to know if the user can access your server? The user may be connected to the internet, but proxies/firewalls may prohibit him to access your site. – imaximchuk Dec 18 '11 at 17:37

2 Answers2

1

Try retrieving something from the internet. Like the document. Or google. Or ping something.

You might want to set a reasonable timeout, or do the check in a background thread on startup (although the connectivity may change, it can be nice to show a little "Connected" icon or something).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    It might not be sufficient. The Internet connection can break (and re-appear) at will. – Basile Starynkevitch Dec 18 '11 at 17:36
  • @BasileStarynkevitch Obviously, which is why I said "the connectivity may change". – Dave Newton Dec 18 '11 at 17:38
  • 1
    You should check the actual web service you are using, not the internet itself. – surfasb Dec 18 '11 at 17:39
  • 1
    @DaveNewton able to connect to google does not mean he is connected to the internet. There is no spec for internet, so u can never say if u are connected to one. The best thing one can do is to check if he is connected to the network where his server resides. – Suraj Chandran Dec 18 '11 at 17:40
  • @SurajChandran Connecting to Google would pretty much imply an internet connection, unless you have a local implementation of Google. Which would be impressive. – Dave Newton Dec 18 '11 at 17:41
  • @DaveNewton how about proxies that return you cached pages? The internet maybe down, but the proxy may return a cached page. On the other hand, even though he is connected to google, the other network where his server resides maybe be blocked, or unreachable or down – Suraj Chandran Dec 18 '11 at 17:42
  • @SurajChandran Your second point is a separate issue from "being conencted to the Internet", and covered by my answer of "retrieve the document" and "ping". Your first point is valid, but hopefully the OP is aware of such things and coding accordingly (and also covered by "pinging"). If there's a proxy, it could return a stale document anyway, so the issue would remain. – Dave Newton Dec 18 '11 at 17:47
0

just ping some well known web resource