本文为大家分享了模拟 ping 操作的一个 java 类 具体内容如下 具体内容如下
import java.io.ioException; import java.net.inetaddress; import java.net.unknownHostException; /** * Creado por Qiuju * el 2014/9/21. */public classping implements runnable {objeto final privado mendlock = new Object (); booleano privado isend = falso; Private int llegando = 0; privado int count; Tiempo de espera privado int; nombre de cadena privada; privado int mendcount; cadena privada mip = null; flotante privado mlossRate = 1f; flotador privado mdelay = 0; public sencillo (nombre de cadena, int count, int timeout) {count = mendCount = count; Tiempo de espera = tiempo de espera; Nombre = nombre; for (int i = 0; i <mendCount; i ++) {Thread Thread = New Thread (this); Thread.SetDaemon (verdadero); Thread.Start (); } if (! isend) {try {sincronizado (mendlock) {mendlock.wait (); }} catch (InterruptedException e) {E.PrintStackTrace (); }}} private void setend (boolean isarrived, long retraso, string ip) {sincronizado (mendlock) {count--; if (isRived) {llegó COUNT ++; mdelay = (mdelay + retraso) / 2f; if (ip! = null) mip = ip; }} if (count == 0) setend (); } private void setend () {mlossRate = (mendCount - ReeleCount) / mendCount; Isend = verdadero; sincronizado (mendlock) {mendlock.notifyall (); }} @Override public void run () {Long demora = 0; boolean isarrived = false; Cadena ip = nulo; intente {long starttime = system.currentTimemillis (); Dirección inetaddress = inetaddress.getByName (nombre); isRived = dirección.isReachable (Tiempo de espera); demora = system.currentTimemillis () - starttime; ip = dirección.gethostaddress (); } capt (desconocidoHosTexception e) {E.PrintStackTrace (); } catch (ioException e) {E.PrintStackTrace (); } catch (Exception e) {E.PrintStackTrace (); } Finalmente {setend (isArved, demandado, ip); }} public String getip () {return mip; } public float getLossRate () {return mlossRate; } public float getDelay () {return mdelay; } public boolean getIsSucEced () {return llegó COUNT> 0; }}在类中使用的是 :
Long Starttime = System.CurrentTimemillis (); Dirección inetaddress = inetaddress.getByName (nombre); isRived = dirección.isReachable (Tiempo de espera); demora = system.currentTimemillis () - starttime; ip = dirección.gethostaddress ();
: : Dirección. Isreachable (Tiempo de espera); 方法 , 但是这个方法有一定局限性;当是 Root 模式下会发送 ICMP 进行 Ping 操作 , 这就比较真实了;但是如果是非 Root 模式下则是使用的 Socket 进行的模拟。
之所以说是中间类 也就是因为这个原因没有采用这个类。 也就是因为这个原因没有采用这个类。
以上就是本文的全部内容 希望对大家的学习有所帮助。 希望对大家的学习有所帮助。