// Simple Java Ping Applet
// Unsupported Freeware by Jay Fenton Aug 1996
// [email protected]
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.lang.Thread;
// The Pinger object measures network latency by sending a packet
// to the UDP Echo Port (Port 7) and timing how long it takes.
// We use this port instead of ICMP because I would have to
// use native methods in order to produce ICMP packets.
class Pinger implements Runnable
{
    static final int echoPort = 7;
    static final int maxPingTime = 3000;     // Milliseconds
    static final int pingPollInterval = 100; // Milliseconds    DatagramSocket socket;
    InetAddress fromIP;
    long sendTime;
    long timeMeasured;
    Thread timeOutMonitor;
    Thread pingListenThread;
    byte packetNumber = 0;    public Pinger(InetAddress pingee)
    {
        fromIP = pingee;
    }
// If needed, start a listener thread to notice the reply.
// then we send out a brief message to the echo port.
// Since the Java thread model does not allow one thread to break
// another one out of a wait, we sleep for brief intervals, waking
// up periodically to see if the reply has come in yet.
    public long doPing() {
        byte[] msg = new byte[1];
        msg[0] = ++packetNumber;
        timeMeasured = -1;        if(socket == null)
            try socket = new DatagramSocket(); catch (Exception e) return(0);        if(pingListenThread == null) {
            pingListenThread = new Thread(this);
            pingListenThread.start();
        }        DatagramPacket packet = new DatagramPacket(msg,msg.length,fromIP,echoPort);
        sendTime = System.currentTimeMillis();
        long timeLimit = sendTime + maxPingTime;        try {
           socket.send(packet);
           while (System.currentTimeMillis() < timeLimit) {
                Thread.sleep(pingPollInterval);
                if(timeMeasured != -1) // reply has been noticed, so return result.
                    return(timeMeasured);
             }
        }
         catch (Exception e) {};
        return(timeMeasured); // return what is probably -1.
    }    // Run method for the listener thread
    public void run() {
     byte[] repBuf = new byte[1];
     DatagramPacket reply = new DatagramPacket(repBuf,repBuf.length);       try
        while (true) {
            socket.receive(reply);
            if(repBuf[0] == packetNumber) {
                timeMeasured = System.currentTimeMillis() - sendTime;
                pingListenThread = null;
                return;
            }
        } catch (Exception e) {pingListenThread = null; return; }
    }    // Clean up any dangling listener thread and release the socket.
    public void stop() {
        if(pingListenThread != null) {
            pingListenThread.stop();
            pingListenThread = null;
        }
      socket.close();
      socket = null;
    }
}public class PingDisplay extends Applet
{
    Pinger ping;
    TextField timeDisplay;
    String fromHost;
    Button refreshButton;    public void init()
    {
     try {
        fromHost = this.getCodeBase().getHost();
  // Alternative for testing on unrestricted browsers.
  //    fromHost = "www.3dcom.com";        ping = new Pinger(InetAddress.getByName(fromHost));        timeDisplay = new TextField("Waiting");
        timeDisplay.setEditable(false);
        this.setLayout(new BorderLayout());
        this.add("Center",timeDisplay);
        refreshButton = new Button("Ping");
        refreshButton.resize(40,20);
        this.add("East",refreshButton);
   } catch (Exception e) {}
    }    public void start()
    {
     super.start();
        displayPing();
    }    public void stop()
    {
       super.stop();
        ping.stop();
    }    void displayPing() {
        timeDisplay.setText("Pinging: " + fromHost); // let user know test underway
        long echoTime = ping.doPing();  // conduct actual test
        if(echoTime == -1)              // check timeout status
            timeDisplay.setText(fromHost + " timed out.");
          else // display time in button
            timeDisplay.setText("Latency to " + fromHost + ": " + Long.toString(echoTime) + " ms.");
    }    // When "Ping" button pressed, rerun and redisplay.
    public boolean action(Event e, Object what) {
        if((e.target == refreshButton) && (e.id == Event.ACTION_EVENT)) {
           displayPing();
           return (true);
        }
        return(false);
    }
}

解决方案 »

  1.   

    telenths(_非法操作_) ,呵呵厉害。
    前天帮我解决了JAVAX。COMM的路径设置问题,在这里多谢了
      

  2.   

    我以前也试过用udp,但是好像不行,能用udp的echo(端口是什么忘了,好象是7或者17)的服务器很少,因此除了在localhost上能成功以外,其他都只返回IP,和request time out.
    to telenths(_非法操作_) :
    你的我试一下
      

  3.   

    这也是我以前写 ping 的时候 参考的一篇文档
    那时候在网上找的翻天覆地 这算是比较好的一个了
    我看他的方法 也是用了 UDP Echo Port (Port 7)
    也许 在 Java 中只能这样做了to cyicecream(小舟) :
      不用客气
      

  4.   

    http://www.3dcom.com/ping/PingDisplay.java
      

  5.   

    http://expert.csdn.net/Expert/topic/1393/1393376.xml?temp=.8255884