本文为大家分享了模拟 Ping 操作的一个 Java 类 , 具体内容如下
Importer java.io.ioException; import java.net.inetAddress; import java.net.unknownhostException; / ** * Créé par Qiuju * le 2014/9/21. * / classe publique Simpleping implémente Runnable {private final objet mendlock = new object (); Boolean privé iSend = false; private int arriveCount = 0; Count int privé; Timeout d'int privé; nom de chaîne privé; Int privé Mendcount; chaîne privée mip = null; float privé mlossrate = 1f; float privé 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 {synchronisé (mendlock) {MendLock.Wait (); }} catch (InterruptedException e) {e.printStackTrace (); }}} private void setend (boolean isarrived, long retard, string ip) {synchronisé (mendlock) {count--; if (isarrived) {arrivantCount ++; mdelay = (mdelay +lay) / 2f; if (ip! = null) mip = ip; }} if (count == 0) setenT (); } private void setend () {mlossrate = (MendCount - arriveCount) / MendCount; ISend = true; synchronisé (mendlock) {MendLock.NotifyAll (); }} @Override public void run () {Long Delay = 0; booléen isarrived = false; String ip = null; essayez {long startTime = System.currentTimemillis (); INETADdress Adresse = InetAddress.getByName (name); isarrived = adresse.isreachable (timeout); Delay = System.CurrentTimeMillis () - STARTtime; ip = adresse.GethostAddress (); } catch (inconnuhostException e) {e.printStackTrace (); } catch (ioException e) {e.printStackTrace (); } catch (exception e) {e.printStackTrace (); } enfin {setend (isarrived, retard, ip); }} public String getIP () {return mip; } public float getlossrate () {return mlossrate; } public float getdelay () {return mdelay; } public boolean getisSucceed () {return arrivantCount> 0; }} :
Long startTime = System.CurrentTimemillis (); INETADdress Adresse = InetAddress.getByName (name); isarrived = adresse.isreachable (timeout); Delay = System.CurrentTimeMillis () - STARTtime; ip = adresse.GethostAddress ();
其中的: adresse.
之所以说是中间类 , 也就是因为这个原因没有采用这个类。
以上就是本文的全部内容 , 希望对大家的学习有所帮助。