GeeCON Prague 2014*
The moose likes Java in General and the fly likes Pinging  IP Addresses... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Pinging  IP Addresses..." Watch "Pinging  IP Addresses..." New topic
Author

Pinging IP Addresses...

Gale Greaser
Greenhorn

Joined: Aug 05, 2002
Posts: 17
Hello!
I'm writing an application that needs to know if a given IP address is being used. Therefore I'm looking to find a the best and fastest way to [perhaps] ping an IP address.
I'm looking for something that works like this:
PingManager ping = new PingManager()
boolean pinged = ping("152.265.45.7");
Right now I'm using HttpURLConnection.connect() to "ping". I'm not getting a warm-fuzzy with this method seeing as it takes a long time to return 'false' if the given IP is not being used.
I would like to know the best way to find if an IP is being used.
I was debating whether or not C++ would be a better candidate for this portion of the code.
Any ideas would be much appriciated.
Thanks in advance!
Gale
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
You can not do a real ping in java, because all java supports is TCP and UDP sockets. Ping is ICMP.
But you can try to open a socket to some well-known port on that machine and at least see if the port is there. But you can't rely on any port being always available.
If the machine is running a web server, then port 80 will probably be there. If it runs ftp, then it'll have port 21. Telnet is 23, sendmail 25, echo 7, etc.
If that's good enough for ya, you can just do:
 
GeeCON Prague 2014
 
subject: Pinging IP Addresses...