http://blog.csdn.net/cgaolei/article/details/4240835
See several forums have been asked to use Java to achieve the function of ping, if Google can also find a lot of answers. Someone uses the Java runtime.exec to invoke the system's ping command directly. Someone has also completed a pure Java implementation ping program, using Java NIO package (native IO, efficient IO package).
I personally think that there is no need to use Java to write a new ping command, because there is not much meaning. More people are concerned with using Java to implement ping to test whether a remote host is available. In fact, the functionality of ICMP Ping has been implemented since the Java 1.5,java.net package. I will introduce the following:
Since Java 1.5, a method in java.net.InetAddress:
public boolean isreachable (int timeout) throws IOException
It implements the ICMP ECHO REQUEST.
Usage is as follows:
String host = "192.168.1.181"
int timeOut = 3000; Timeout should be over 3 banknotes
Boolean status = Inetaddress.getbyname (host). Isreachable (TimeOut);
When the return value is true, the host is available and is not.
It should be noted that if the remote server has a firewall or associated configuration, it may affect the results and is unavoidable.
In addition, the Isreachable method attempts to connect to the TCP Port 7 (Echo) of the remote host, because the program has certain permissions to the system for sending ICMP requests, and when this permission is not satisfied.
Method Two
Use the runtime execute cmd command. This has the Chinese garbled problem, has not solved for the time being, later will say again.
Runtime Runtime = Runtime.getruntime ();
Process process = NULL;
String command = "Ping 10.16.36.29";
String str = "";
try {
Process = runtime.exec (command);
InputStreamReader InputStreamReader = new InputStreamReader (Process.getinputstream ());
Char[] by = new char[1024];
while (Inputstreamreader.read (by)!=-1) {
System.out.println (A new String (by));
}
catch (IOException e) {
E.printstacktrace ();
}