File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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>
 
jQuery in Action, 2nd edition
 
subject: How to know that my system have Internet connectivity through JAVA
 
Similar Threads
to identify internet connection
plz answer
running on internet??
Configuration for sending emails
Please anyone reply my questions