Two Laptop Bag*
The moose likes Sockets and Internet Protocols and the fly likes Ping 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 "Ping" Watch "Ping" New topic
Author

Ping

Mark Hogan
Greenhorn

Joined: Jun 24, 2013
Posts: 4
Hi
Which class and method of java.net package can do ping an ip address ?
Thanks
B. Katz
Ranch Hand

Joined: Aug 28, 2009
Posts: 51

Greetings,

Well, you have the class, java.net.InetAddress, now you need the method. For example:

... where "4000" is the timeout.

HTH. Let us know.


"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use."
-- Galileo Galilei
Peter Sorotokin
Ranch Hand

Joined: Jun 25, 2013
Posts: 36
    
    1

Hi
Does it return a boolean value ? What does timeout mean in this code ?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Mark Hogan wrote:Hi
Which class and method of java.net package can do ping an ip address ?
Thanks


'ping' uses the ICMP protocol and last time I looked this was not implemented in the core Java networking libraries. There may be a third party library to do this but the chances are it will use JNI.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

Peter Sorotokin wrote:
Does it return a boolean value ? What does timeout mean in this code ?


Well, what does the javadoc for java.net.InetAddress say about what is returned from the isReachable() method?

As for the timeout, think of it like this... if the machine is reachable then it will respond saying that it is there. However, what happens if it is not reachable? Can it respond saying that it is not there? .. And what if it is reachable, but really slow because it has to go through routers? How many times and how long should you keep trying? ... anyway, that is what the timeout is for. In this example, the program will keep trying for four seconds, and assumes unreachable, if it doesn't get a response within that time.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Peter Sorotokin
Ranch Hand

Joined: Jun 25, 2013
Posts: 36
    
    1



It produce this Error :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type UnknownHostException
Unhandled exception type IOException

at Ping.main(Ping.java:7)


What is wrong ?
Thanks
Peter Sorotokin
Ranch Hand

Joined: Jun 25, 2013
Posts: 36
    
    1



It produces this error :

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from int to byte
Type mismatch: cannot convert from int to byte

at Ping.main(Ping.java:6)


I know that byte type is between -127 and +127
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

Peter Sorotokin wrote:

It produce this Error :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type UnknownHostException
Unhandled exception type IOException

at Ping.main(Ping.java:7)



In my opinion, it is *not* a good idea to run a program that doesn't compile !! The java compiler and jvm provided by the jdk doesn't allow this -- so, obviously, you are using an IDE.

Please make sure that your program compiles before running them... and if you must do it, then please report the exact error that is provided by the compiler.

Henry
Peter Sorotokin
Ranch Hand

Joined: Jun 25, 2013
Posts: 36
    
    1

Thanks Henry
It isn't compiled by JDK and produced this error:


[Thumbnail for untitled.JPG]

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

Peter Sorotokin wrote:Thanks Henry
It isn't compiled by JDK and produced this error:



Yeah... we kinda already know what the error is.... We just assumed that you fixed it with the runtime message -- even though you should have used the compiler error message.


Anyway, do you know what a checked exception is? And what you need to do with it?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

Peter Sorotokin wrote:
I know that byte type is between -127 and +127


The range of a signed byte is from -128 and +127. And also, the same request about posting the compiler message for this error too.

Henry
Peter Sorotokin
Ranch Hand

Joined: Jun 25, 2013
Posts: 36
    
    1



It finally worked . Thanks Henry . Thanks to others
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18491
    
  40

Peter Sorotokin wrote:
It finally worked . Thanks Henry . Thanks to others



Thank you for posting the solution -- you would be surprised on how often someone ask a question, get help, solve the problem, and don't even bother telling us that the problem is solved.

Posting the answer is highly appreciated -- in fact, here is a cow for the good deed...

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ping
 
Similar Threads
test
How to measure responsetime of network using JAVA?
Scrabble... it only takes a minute...
Change hostname from localhost to something in Tomcat
Design pattern for sorting nested arraylists