It seems that the use of javaping is very rare, but I met; Finally, the class in the middle process is posted here.
Import Java.io.ioexception;import java.net.inetaddress;import java.net.unknownhostexception;/** * Created by QiuJU * on 2014/9/21. */public class Simpleping implements Runnable {Private Final object mendlock = new Object (); Private Boolean isend = false; private int arrivedcount = 0; private int Count; private int TimeOut; Private String Name; private int mendcount; Private String mIp = null; Private float mlossrate = 1f; private float mdelay = 0; Public simpleping (String name, int count, int timeOut) {count = Mendcount = count; Timeout = timeout; name = name; for (int i = 0; i < Mendcount; i++) {Thread thread = new Thread (this); Thread.setdaemon (TRUE); Thread.Start (); } if (! Isend) {try {synchronized (mendlock) {mendlock.wait (); }} catch (Interruptedexception e) {e.printstacktrace (); }}} private void SetEnd (Boolean isarrived, long delay, String IP) {synchronized (Mendlo CK) {count--; if (isarrived) {arrivedcount++; Mdelay = (Mdelay + delay)/2f; if (IP! = null) mIp = IP; }} if (Count = = 0) setend (); } private void SetEnd () {mlossrate = (mendcount-arrivedcount)/mendcount; Isend = true; Synchronized (Mendlock) {mendlock.notifyall (); }} @Override public void Run () {long delay = 0; Boolean isarrived = false; String IP = null; try {Long startTime = System.currenttimemillis (); InetAddress address = Inetaddress.getbyname (Name); isarrived = address.isreachable (TimeOut); Delay = System.currenttimemillis ()-startTime; ip = address.gethostaddress (); } catch (UnknownhoStexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } catch (Exception e) {e.printstacktrace (); } finally {SetEnd (isarrived, delay, IP); }} public String GetIP () {return mIp; } public float Getlossrate () {return mlossrate; } public float Getdelay () {return mdelay; } public boolean getissucceed () {return arrivedcount > 0; }}
Used in the class are:
Long startTime = System.currenttimemillis (); InetAddress address = Inetaddress.getbyname (Name); isarrived = address.isreachable (TimeOut); Delay = System.currenttimemillis ()-startTime; ip = address.gethostaddress ();
Among them: Address.isreachable (TimeOut); method, but this method has some limitations; when the ICMP is sent to ping in root mode, it is more realistic. However, if you are using a socket in non-root mode, the simulation is performed.
That's the middle class, which is why this class is not used for this reason.
It's a record to post it here.
Of course if you want to know the final solution, I can tell that the command line operation is used and then the parsing is done.
Share a class for simulating ping operations in Java