This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How to know that my system have Internet connectivity through JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to know that my system have Internet connectivity through JAVA" Watch "How to know that my system have Internet connectivity through JAVA" New topic
Author

How to know that my system have Internet connectivity through JAVA

fundu man
Greenhorn

Joined: Jan 10, 2007
Posts: 3
Hi,

How can I write a JAVA code which can check that my system have INTERNET connection or not ?

regards,
Fundu
Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Hi Fundu,

This is a bit of a kludge, but the following code will throw an UnknownHostException if you do not have an internet connection. Without a connection, the name cannot be resolved.


Life is good on the Ponderosa,<br />but mind where you step.
Sandeep Deb
Greenhorn

Joined: Jun 01, 2007
Posts: 15
Unfortunately, the above solution has some drawbacks. The hosts file can override the name to IP lookup and will also work in a disconnected mode. So for example, if I change my hosts file on c:\windows\system32\drivers\etc (note, there is another hosts file on c:\windows\i386) and and entry mapping www.gooogle.com to 2.3.4.5, the above code would return 2.3.4.5 even if my machine is unplugged.

I suggest using the following code to ascertain if your computer is connected to the network.



Of course you will have to made ammends for a proxy server, if any. Note that this is a blocking call and blocks for approx 5 seconds if no network is present.

Hope this helps.


- <a href="http://www.sandeepdeb.com" target="_blank" rel="nofollow">Sandeep</a>
 
 
subject: How to know that my system have Internet connectivity through JAVA