類比Ping操作的一個Java類_java

來源:互聯網
上載者:User

本文為大家分享了類比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 < 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 setEnd(boolean isArrived, long delay, String ip) {  synchronized (mEndLock) {   Count--;   if (isArrived) {    arrivedCount++;    mDelay = (mDelay + delay) / 2f;    if (ip != null)     mIp = ip;   }  }  if (Count == 0)   setEnd(); }  private void setEnd() {  mLossRate = (mEndCount - arrivedCount) / mEndCount;   IsEnd = true;  synchronized (mEndLock) {   mEndLock.notifyAll();  } }  @Override public void run() {  long delay = 0;  boolean isArrived = false;  String ip = null;  try {   long startTime = System.currentTimeMillis();   InetAddress address = InetAddress.getByName(Name);   isArrived = address.isReachable(TimeOut);   delay = System.currentTimeMillis() - startTime;   ip = address.getHostAddress();  } catch (UnknownHostException e) {   e.printStackTrace();  } catch (IOException e) {   e.printStackTrace();  } catch (Exception e) {   e.printStackTrace();  } finally {   setEnd(isArrived, delay, ip);  } }  public String getIp() {  return mIp; }  public float getLossRate() {  return mLossRate; }  public float getDelay() {  return mDelay; }  public boolean getIsSucceed() {  return arrivedCount > 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進行的類比。
之所以說是中間類,也就是因為這個原因沒有採用這個類。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.