本文为大家分享了模拟ping操作的一个java类,具体内容如下
导入java.io.ioexception;导入java.net.inetaddress;导入java.net.unknownhostexception; /** *由Qiuju *于2014/9/21创建。 */公共类简单实现运行{私有最终对象mendlock = new Object();私人布尔iSend = false;私人int到达= 0;私人INT计数;私人int超时;私有字符串名称;私人int mendcount;私有字符串mip = null;私有浮子mlossrate = 1f;私有浮点mdelay = 0;公共简单(字符串名称,int count,int timout){count = mendCount = count;超时=超时;名称=名称; 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 steTend(boolean isarried,长延迟,字符串ip){同步(mendlock){count---;如果(已多默接){到达++; mdelay =(mdelay + delay) / 2f; if(ip!= null)mip = ip; }} if(count == 0)setend(); } private void setend(){mlossrate =(MendCount -resercount) / mendcount; isend = true;同步(Mendlock){Mendlock.notifyall(); }} @Override public void run(){long delay = 0;布尔人属于= false;字符串ip = null;尝试{long starttime = system.currenttimemillis(); InetAddress地址= InetAddress.getByName(name); isarried = address.israchable(超时); delay = system.currentTimeMillis() - 启动时间; ip = address.gethostaddress(); } catch(unknownhostexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } catch(异常E){e.printstacktrace(); }最后{setend(已交配,延迟,ip); }} public String getip(){return mip; } public float getLossrate(){返回mlossrate; } public float getDelay(){return mdelay; } public boolean getIssucceed(){返回到达> 0; }}}在类中使用的是:
长启动时间= system.currentTimeMillis(); InetAddress地址= InetAddress.getByName(name); isarried = address.israchable(超时); delay = system.currentTimeMillis() - 启动时间; ip = address.gethostaddress();
其中的:地址。iSREACHABLE(超时);方法,但是这个方法有一定局限性;当是 root root模式下会发送icmp ping操作,这就比较真实了;但是如果是非
之所以说是中间类,也就是因为这个原因没有采用这个类。,也就是因为这个原因没有采用这个类。
以上就是本文的全部内容,希望对大家的学习有所帮助。,希望对大家的学习有所帮助。