Ping in java
Hi,
I am trying to ping a host through java,
here are the 3 ways am trying to achieve, first one works fine, but i do not want to use it.
1)
String ip = "127.0.0.1";
Process p = Runtime.getRuntime().exec("ping " + ip);
int status = p.waitFor();
System.out.println(ip + " is " + (status==0 ? "alive" : "dead"));
2)
InetAddress address = InetAddress.getByName("somemachine.somenetwork.com");
if(address != null ){ System.out.println("address created");System.out.println("isreachable == " + address.isReachable(timeout));
} else
System.out.println("unreachable");
This kind of behaves weird or I have got my understanding wrong,
I think isReachable() uses ICMP echo/request to simulate ping,
but this does not work for me, isReachable() returns true for some cases and false for some, I have tried increasing the timeout too,
it doesnt even work for my localhost, not sure whats wrong, any ideas?
Also the hosts for which it returns false gets recognised via ping on the command prompt and the first case(obviously)
3)
Socket t = new Socket("127.0.0.1", 7);
BufferedReader br = new BufferedReader (new InputStreamReader(t.getInputStream()));
PrintStream ps = new PrintStream(t.getOutputStream());
ps.println("Hello");
String str = br.readLine();
if (str.equals("Hello"))
System.out.println("Alive!") ;
else
System.out.println("Dead or echo port not responding");
t.close();
here am trying to create a socket on the host on port 7(echo port)
This gives me the foll exception with any host:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
Is there anything obviously wrong am doing here?
Please give me more info!
thanks!
[2348 byte] By [
veeseekaya] at [2007-11-27 0:34:54]

# 1
You doing right but isReachable() and Socket("127.0.0.1", 7) both means simple connect to port 7 on remote host. In most cases this port closed and you'll get "unreachable" or "refused".
# 2
So would I be right in saying "unless port 7 isnt closed, the only way to ping a host would be thru Runtime.exec()"if thats true, how does ping work? I thought isReachable() does the same thing as ping.thanks again!
# 3
If only three cases above present then yes - use exec().Ping uses ICMP packets while isReachable() makes simple connect attempt to port 7.
# 4
from the API text....."
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."
I use this method in my app and works fine... i tested with console ping and got the same results, but always with a timeout of 1000ms at least . less than it will fail...
# 5
thanks ppl! that clears the air a little bit,
but am surprised at the way isReachable() behaves at times, because it says unreachable for the hosts that can be reached through command line ping(i give sufficient timeout value too)...
Because of which I am forced to use Runtime.exec in my application.
glad isReachable() works for maximilan.
cheers!
# 6
You are rigth, for example on some hosts in my network doesn磘 work due to the firewall... no because the method do not work... is a simple way to do it... but to get a delay timeout... i use the runtime... and handle the ouput from system ping...
# 7
my similar problem occured only on Windows machines.
maybe don't assume windows allows java to create real icmp packets.
(and maybe that explains why your system calls work).
if the problem machine is Windows,
ftp the byte code to other os and give it a try.
otherwise, i've no ideas.
Message was edited by:
dew2hiroo
# 8
just to let you know.. my app work with cisco routers and sun spark servers and windows 2000 servers... i tested zillion times and still working.. there are two sun blade servers totally equal... one respond and the other not.... this is because the firewall....and i have to use static route...for that..
# 9
Hi everyone,
I m also facing the same problem while trying to ping to different ip address.
I have implemented the ping feature using Runtime. But it is showing strange behaviour. I have multiple threads (around 250 or so) running simultaneously trying to ping different IPs using:
Runtime r = Runtime.getRuntime();
Process p = r.exec("ping" + address);
But I think, when so many threads tries to ping using runtime and because getRuntime is static it is not giving me proper response even if all the ip were pingable when I tried using them by command prompt.
What I need to do?
Please help me. ASAP.
Thanks in advance!!!
# 10
It has nothing to do with Runtime.getRuntime() being static. Ping isn't a concurrent protocol because ICMP doesn't have ports. Why do you have to do this?
ejpa at 2007-7-11 22:43:00 >

# 11
ok. but what can i do here?
I have multiple jobs in a quartz scheduler and every job creates more than 25 threads and these threads calls there own instance of class which is responsible to ping the given IP using the same Runitme.getRunTime() and send the response back. Thus, at a given time there may be as many as 150 threads were running and trying to ping there own set of IPs.
When I tried this whole process for only 1 job in quartz, every IP gets pinged, but as soon as I start the whole execution with more number of jobs, these IPs stops giving the response (Ping class couldnt be able to ping those IPs) even if they are in the network.
Please provide some help....
Thanks
# 12
I already asked this. Why do you have to do this? The best way to tell whether a resource is available is to try to use it - in this case, to try to connect to the server. Pinging is only a guess, and the indication it returns is instantly out of date.
ejpa at 2007-7-11 22:43:00 >

# 13
I thought I have only one option to check whether that network component is up or not. Am I taking u right?If i m wrong....Can you please tell me...what exactly i need to do here.May be I m not getting the core point rightly. Please, provide some help...Thanks.
# 14
When I ask a question twice I need an answer, not another question.
ejpa at 2007-7-11 22:43:00 >

# 15
yes you are right..But until and unless i wont get ur question correctly how come i'll answer that.... That is why I wrote..."may be i m not getting u rightly"
# 16
Three times now. Why do you have to do this? Why do you have to check whether a network component is up?
I don't think the question is exactly unclear,and I certainly can't help you until you answer it, except to say that you're trying to do something that is probably (a) impossible (see reply #10) and (b) unnecessary (see reply #12).
ejpa at 2007-7-21 19:49:18 >

# 17
Ok.
We have n number of networks deployed (wireless network).
Now in every n/w we have x number of nodes (access points).
Every access point has there own snmp agent running which are actually responsible to receive and transmit data in the n/w. Depending upon whether all these nodes are up/down we are maintaining the list of these nodes.
Now i want to check whether any particular node is inside the n/w by actually pinging that node. If I get the response this means the node is in n/w otherwise not...
# 18
So your best bet is to maintain a permanent connection to each SNMP service and periodically send it a null transaction with a shortish read timeout. If you get the timeout, close the connection and create a new one, again with a shortish timeout. If either of these timeouts occurs, the node is suspect, otherwise not only is the network node up, so is the SNMP service, which is what you really need to know. This solution (a) doesn't suffer from timing window problems, (b) is concurrent, multi-threaded, etc, (c) is implementable in 100% pure Java, and (d) bears some relationship to your actual problem. Unlike just trying to ping.
ejpa at 2007-7-21 19:49:18 >

# 19
Friends, my issue is how to ping a server in remote machine in the particular port where it is running. It is a stream socket server which is running on a definite port. I need to ping and test , that server is running or not?
By using InetAddress.isReachable(timeout), we can ping the remote system but not that particular server's port. Can anyone help me in this regard?
thx
Ganesh
# 20
Connect to the port. Google for java socket tutorial to see how to establish a TCP/IP connection.
# 21
here is simple example of how to use socket
connections to ping servers.
byte inet[] = InetAddress.getLocaHost().getAddress();
for(int i = 1; i < 255; i++) {
inet[3] = Integer.valueOf(i).byteValue();
InetAddress inetAddress = InetAddress.getByAddress(inet);
SocketAddress sokAddress = new InetSocketAddress(inetAddress, 3306);
Socket sok = new Socket();
try {
sok.connect(sokAddress, 2);
System.out.println("server is up and running at > " + sok);
} catch(SocketTimeoutException e) {}
}
i hope that helps.
# 22
I also have the same problem.How to check if the remote resource is up or not?
# 23
What is wrong with all the answers already in this thread?
# 24
> simple example
hmm ...
> inet[3] = Integer.valueOf(i).byteValue();
inet[3] = (byte)i;
is somewhat simpler.
> SocketAddress sokAddress = new InetSocketAddress(inetAddress, 3306)
Assumes there is something listening on port 3306. Is there?
>sok.connect(sokAddress, 2);
Two milliseconds is an absurdly short timeout setting. You'd be lucky if the localhost could respond that quickly. Two hundred mllliseconds would be too short.
Is there something wrong with just trying to connect with what you're trying to connect to? with a timeout if necessary? or at worst is there something wrong with using InetAddress.isReachable()?
ejpa at 2007-7-21 19:49:18 >
