Share a class for simulating ping operations in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.