微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

模拟Ping操作的一个Java类

这篇文章主要为大家详细介绍了一个模拟Ping操作的java类,感兴趣的小伙伴们可以参考一下

本文为大家分享了模拟Ping操作的一个java类,具体内容如下

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 0; } }

在类中使用的是:

long startTime = System.currentTimeMillis(); InetAddress address = InetAddress.getByName(Name); isArrived = address.isReachable(TimeOut); delay = System.currentTimeMillis() - startTime; ip = address.getHostAddress();

其中的:address.isReachable(TimeOut);方法,但是这个方法有一定局限性;当是Root模式下会发送ICMP进行Ping操作,这就比较真实了;但是如果是非Root模式下则是使用的Socket进行的模拟。

之所以说是中间类,也就是因为这个原因没有采用这个类。

以上就是本文的全部内容,希望对大家的学习有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐