1

I want to get the delay time between 2 PC by this way:

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

but the results always be "false" except "localhost",I tried change the getByName("192.168.6.18") to the PC of LAN ,or the website like "www.facebook.com",but it has no effect

wyyzaw
  • 51
  • 1
  • 5

1 Answers1

-3

Use this method for 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;
 }

for more details see Ping class

For PC other than windows use

  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;
}
Community
  • 1
  • 1
prem30488
  • 2,828
  • 2
  • 25
  • 57
  • And what if his OS has no `ping` command? And what if the result from the `ping` does not contain English text? This is not scalable solution. – qqilihq Apr 06 '14 at 18:32
  • This is not a good general solution. (1) The `ping` command may not exist. (2) The `ping` output may not be in that format - OS X will say `x bytes from`, and on non-English versions of Windows the messages will be translated into the local language. – nobody Apr 06 '14 at 18:33
  • Falling back to the same method that the OP says isn't working is not helpful. – nobody Apr 06 '14 at 18:51
  • 1
    Also, beware that some implementations of `ping` do not terminate (until you send a signal such as SIGINT). Heavy use of this could fill your process table and end up generating a *lot* of traffic. – nobody Apr 06 '14 at 18:56
  • I agree with your knowledge but it does not make sense to vote down my answer... it contains a solution. – prem30488 Apr 06 '14 at 18:58
  • It is a very low quality solution, for all the reasons I have listed. Down-voting is appropriate for low quality answers. – nobody Apr 06 '14 at 19:01