1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import java.io.*;
import java.net.*;
import java.nio.channels.*;
public class Ping {
private static int timeout; // Timeout in ms for pinging
private static Target t;
private static SocketChannel sc;
// Representation of a ping target
private static class Target {
public InetSocketAddress address;
public SocketChannel channel;
public long connectStart;
public long connectFinish = 0;
public Exception failure = null;
public Target(String host, int port) throws IOException
{
address = new InetSocketAddress(InetAddress.getByName(host), port);
}
public long getPing() throws IOException, InterruptedException
{
if (connectFinish != 0)
return connectFinish - connectStart;
else if (failure != null)
if (failure instanceof IOException) throw (IOException) failure;
else if (failure instanceof InterruptedException)throw (InterruptedException) failure;
else
return -2; //should not happen |
Partager