本文为大家分享了模拟 ping 操作的一个 java 类 , 具体内容如下
importar java.io.ioException; importar java.net.inetaddress; importar java.net.unknownhosTexception; /** * Criado por Qiuju * em 2014/9/21. */public class Simpleping implementa runnable {private final objeto mendlock = new Object (); private boolean isend = false; Private Int chegou = 0; Parte INT privada; Timeout privado int; nome de string privado; privado int mendcount; String privada mip = null; Mlossrate de flutuação privado = 1F; flutuador privado mdelay = 0; public Simpleping (nome da string, int conting, int timeout) {count = mendCount = count; Tempo limite = tempo limite; Nome = nome; for (int i = 0; i <mendcount; i ++) {thread thread = new Thread (this); thread.setDaemon (true); thread.start (); } if (! isend) {try {sincronizado (mendlock) {mendlock.wait (); }} catch (interruptEdException e) {e.printStackTrace (); }}} private void setend (boolean isarrivado, longo atraso, string ip) {sincronizado (mendlock) {count--; if (isarrivado) {chegou o count ++; mdelay = (mdelay + atraso) / 2f; if (ip! = null) mip = ip; }} if (count == 0) setend (); } private void setEnd () {mlossrate = (mendcount - chegou) / mendcount; Isend = true; sincronizado (mendlock) {mendlock.notifyall (); }} @Override public void run () {Long Atraso = 0; boolean isarrived = false; String ip = null; tente {long starttime = System.currenttimemillis (); Endereço inetaddress = inetaddress.getbyName (nome); isarrived = endereço.isreachable (tempo limite); Atraso = System.CurrentTimemillis () - StartTime; ip = endereço.gethostAddress (); } catch (desconhecidohostException e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); } catch (Exceção e) {e.printStackTrace (); } finalmente {setend (isarrivado, atraso, ip); }} public string getip () {return mip; } public float getLossrate () {return mlossrate; } public float getDelay () {return mdelay; } public boolean getissucceced () {return chegou a chegada> 0; }}在类中使用的是
long startTime = System.currenttimemillis (); Endereço inetaddress = inetaddress.getbyName (nome); isarrived = endereço.isreachable (tempo limite); Atraso = System.CurrentTimemillis () - StartTime; ip = endereço.gethostAddress ();
其中的 : Endereço.
之所以说是中间类 , 也就是因为这个原因没有采用这个类。
以上就是本文的全部内容 , 希望对大家的学习有所帮助。