aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes status of internet connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "status of internet connection " Watch "status of internet connection " New topic
Author

status of internet connection

mahdi farzami
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Hi , how can know in java that pc is connected to internet or no ??? i write this code
but it`s when user is root work true !!!
Rohan kanade
Ranch Hand

Joined: Oct 22, 2009
Posts: 106
If you just want to know if the pc is connected to internet or not, ping www.google.com or some other very common host, like some DNS server, and check if the ping is replying or not.


SCJP 1.6 ,SCWCD 5
mahdi farzami
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
If you just want to know if the pc is connected to internet or not, ping www.google.com or some other very common host, like some DNS server, and check if the ping is replying or not.

how can i do this in my app ???
Rohan kanade
Ranch Hand

Joined: Oct 22, 2009
Posts: 106
String host = "commonhosts[randomnumber]";

where commonhosts is a string array containing some of the common hosts like www.google.com or dns servers which are always up.

randomnumber should be generated by you to choose from the commonhosts.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

mahdi farzami wrote:
If you just want to know if the pc is connected to internet or not, ping www.google.com or some other very common host, like some DNS server, and check if the ping is replying or not.

how can i do this in my app ???


by doing a google search on "java ping"
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Tim McGuire wrote:
mahdi farzami wrote:
If you just want to know if the pc is connected to internet or not, ping www.google.com or some other very common host, like some DNS server, and check if the ping is replying or not.

how can i do this in my app ???


by doing a google search on "java ping"


Forgive me. I responded too hastily. For several reasons, the information from a google search on this subject is confusing.

What I found out after following my own advice is that isReachable() is not a fail-proof way to ping something.

This bit of code worked for me:
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149


the reason for this program "only working as root" is:

  • when running as root, Java uses ICMP, as ping does


  • when running as non-privileged user, Java uses ECHO (port 7)


  • try: telnet 209.85.229.147 7
    TCP times out, like your Java program

    and as you said, ping (ICMP) works

    But that server is listening on port 80, so why not connecting there?

    Hope that helps
    Matt
     
    Don't get me started about those stupid light bulbs.
     
    subject: status of internet connection