这是一个ping的例子,你自己拿去研究吧。telnet就比较麻烦了,因为telnet可不仅仅是一条命令,它代表切换到一种环境,相应的会有一系列指令集需要自己实现哦// Simple Java Ping Applet
// Unsupported Freeware by Jay Fenton Aug 1996
// [email protected]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.lang.Thread;
import SAL.util.unbug;
// 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);
   unbug.hex_dump ("Sending", msg, msg.length);
           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);
    unbug.hex_dump ("Reply", repBuf, repBuf.length);             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
implements ActionListener
{
    Pinger ping;
    TextField timeDisplay;
    String fromHost;
    Button refreshButton;    public void init()
    {
     try {
        fromHost = this.getCodeBase().getHost();
  // Alternative for testing on unrestricted browsers.
  //    fromHost = "albee.sal.wisc.edu";        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.setSize(40,20);
        refreshButton.addActionListener(this);
        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 void actionPerformed (ActionEvent e) {
       displayPing();
    }
}

解决方案 »

  1.   

    runtime的exec只有此方法,socket 7端口 通信,和ping还是有区别的然后通过捕捉返回信息,获得数据。
      

  2.   

    PING和TELNET还不一样。
    TELNET运行在TCP上的应用协议,PING是运行在ICMP上的,ICMP与IP协议关系很特殊,他是IP上层协议,但是他也是IP协议不可缺少的一部分
    你要实现ICMP就必须要了解更多下层协议的东西,技术上相对TELNET要困难些,毕竟,在一般的系统中运行
    JVM的权限不是很高,很难达到核心层,所以要实现ICMP包难度比较大,不知道是不是给你安排任务的人不了解IP协议规范吗还是怎么,居然给这样的根本没有必要的东西
      

  3.   

    如果你使用本地代码做ICMP,还要JAVA做什么,还不如用其他语言用系统内核调用来完成更加直接
      

  4.   

    直接系统调用不行吗?import java.io.*;public class Ping
    {
         public static void main(String args[])
        { 
            try{
              Process pro = Runtime.getRuntime().exec("cmd /c  start ping 192.168.0.1 -t"); 
            }
    catch(Exception ex)
    {
    System.out.println(ex);
    }
        }
    }telnet的类似。