Meaningless Drivel is fun!
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pinging  IP Addresses..." Watch "Pinging  IP Addresses..." New topic

Pinging IP Addresses...

Gale Greaser

Joined: Aug 05, 2002
Posts: 17
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("");
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!
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:
I agree. Here's the link:
subject: Pinging IP Addresses...
It's not a secret anymore!