1

この方法で2台のPC間の遅延時間を取得したい:

currentTime = System.currentTimeMillis();           
isPinged = InetAddress.getByName("192.168.6.18").isReachable(2000);                 
currentTime = System.currentTimeMillis() - currentTime;
System.out.println("----"+isPinged+":"+currentTime);

しかし、結果は常に「localhost」以外は「false」getByName("192.168.6.18")なので、LANのPCに変更したり、「www.facebook.com」などのWebサイトに変更したりしましたが、効果がありません

4

1 に答える 1

-3

Windows PC ではこの方法を使用します

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

public Boolean ping(String ipaddress)
{
    Runtime runtime = Runtime.getRuntime();
    String cmds = "ping "+ipaddress;
    System.out.println(cmds);
    Process proc;
    try {
         proc = runtime.exec(cmds);
        proc.getOutputStream().close();
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String line;
        while ((line = bufferedreader.readLine()) != null) {
            if(line.contains("Reply from "+ipaddress+":"))
                {
                return true;    
                }
            }
        }catch (IOException e) {
    e.printStackTrace();
    }
    return false;
 }

詳細については、Ping クラスを参照してください

Windows以外のPCの場合

  public Boolean IsReachable(String ipaddress) {
    try {            
        final InetAddress host = InetAddress.getByName(ipaddress);

        try {
            return host.isReachable(3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return false;
}
于 2014-04-06T18:25:20.747 に答える