Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

status of internet connection

 
mahdi farzami
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
mahdi farzami
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic