Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ping a IP address in Java

 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code:


try 3. { 4. // Google.com 5. if (InetAddress.getByAddress("173.194.35.133".getBytes()).isReachab le(1000)==true) 6. { 7. //Boolean variable named network 8. network=true; //Ping works 9. } 10. else 11. { 12. network=false; //Ping doesnt work 13. } 14. } 15. catch (UnknownHostException e3) 16. { 17. System.out.println("ERROR: DNS server error " + e3.getLocalizedMessage()); 18. } 19. catch (IOException e3) 20. { 21. System.out.println("ERROR: Some other error " + e3.getLocalizedMessage()); 22. 23. }

The reason I use a IP address instead of a more common host name is because I also have to ping a internal address below. The code does not seem to work. I also tried byName and directly putting the IP but nothing.

Any hints or tips?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that code? It is quite illegible. Please go back to your post and use the edit button to make it legible. Indentation, whitespace, new lines, etc. all required. And no line numbers, please.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I apoligize for that. On my Android, it looked formatted.........I guess I posted it wrong or something.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted
I have removed the code tags from the first post, which believe it or not, makes it easier to read.

You can probably reduce that to
network = InetAddress.getByAddress("173.194.35.133".getBytes()).isReachable(1000);
Never write == true or == false. Don’t use if-elses to set boolean values. It is rather like this: see ยง10.5.2.
Don’t know any more. Are you suffering an Exception? What is its stack trace and message? Try a print statement immediately after that line, to print the value of network, which will be omitted if there is an Exception.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This



Throws a exception. UnknownHostException to be exact.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"173.194.35.133".getBytes() does not do what you think it does. Or if it does do what you think it does, then you are mistaken in what you think InetAddress.getByAddress is expecting as a parameter.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:"173.194.35.133".getBytes() does not do what you think it does.


And once you clear that up, be aware that isReachable() is next to useless, as far as I undrestand it. I'm not sure of the details. Here's what I think is going on though. The docs say:
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.


The problem is that either a) Java doesn't actually even try those ICMP ECHOs (or at least didn't) or else is unable to get permission, and b) Most hosts don't actually run the echo server on port 7.

In general, just pinging to see if a host is there in Java isn't of much value, unless you're trying to write a program that does a survey of existing hosts. If you're pinging as a pre-connection test of availability, just skip it. It's pointless, and the result can change between the ping and the connection. Just try to connect, and if it succeeds, the host is obviously reachable.

If you really need to do a ping reliably, you'll probably have to get a native library that you can use for raw socket requests and deal with the ICMP packets yourself. (I think jpcap may help with this, but I'm not sure.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for “beginning”. Moving thread.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kind of disappointing Java cant do this byitself........

I check to see if I have internet access to send a email or not..........I check to see if I have LAN/intranet access, to see if I FTP or not........

Instead of this, do you recommend I control this via exceptions that are thrown? I see that as kind of "sloppy".......
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff already said it.
Just try to send the email. If there is no internet connection available it is the responsibility of the eMail Class or whatever you may call it to tell you its not possible at this time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koldo Urrutia wrote:Kind of disappointing Java cant do this byitself........

I check to see if I have internet access to send a email or not..........I check to see if I have LAN/intranet access, to see if I FTP or not........

Instead of this, do you recommend I control this via exceptions that are thrown? I see that as kind of "sloppy".......


No.

As I already stated, checking first is the wrong approach.

Catching the exception is not sloppy. It's the right way to do it. It's what exceptions are there for. And you'd have to do it anyway, even if you got a successful ping before connecting. The connectivity could be lost between the ping and when you connect, or while your connection is active. Once again: Pinging to determine connectivity prior to connecting is pointless.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koldo Urrutia wrote:Kind of disappointing Java cant do this byitself........


I can see two possible reasons why not:

1) There just isn't that much call for it in the kinds of applications Java is usually used for. About the only time I see anybody asking about raw sockets or ICMP is because they want to implement ping in Java, and then it's usually for the wrong reason--to check if a server is there before trying to connect. So it wouldn't make much sense to put in full-blown raw socket support (which is really pretty complex), and even if they didn't add the full-blown support to the API and just provided a direct ping method, there's rather a lot of native code needed just to support that.

2) Security reasons. On both Windows and Linux, I don't think normal users are allowed to create raw sockets. I think the Windows version of jpcap has a DLL that has to run as administrator, or switch to supervisor mode, or whatever the Windows terminology for that is, and on Linux, the ping executable is owned by root and has the setuid flag set. When you execute ping, the effective user is set to root--normal users wouldn't have the permissions needed for ICMP packets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic