package AVS3;
import java.io.*;
import java.net.*;
public class Ping2 {
public static long testConn(byte[] addr1, int port, int timeoutMs){
//pass in a byte array with the ipv4 address, the port & the max time out required
long start = -1; //default check value
long end = -1; //default check value
long total = -1; // default for bad connection
//make an unbound socket
Socket theSock = null;
try {
InetAddress addr = InetAddress.getByAddress(addr1);
System.out.println(addr);
SocketAddress sockaddr = new InetSocketAddress(addr,port);
//Create the socket with a timeout
//when a timeout occurs, we will get timout exp.
//also time our connection this gets very close to the real time
theSock = new Socket();
start = System.currentTimeMillis();
System.out.println("start:"+start);
theSock.connect(sockaddr, timeoutMs);
end = System.currentTimeMillis();
System.out.println("end:"+end);
} catch (UnknownHostException e) {
start = -1;
end = -1;
} catch (SocketTimeoutException e) {
start = -1;
end = -1;
} catch (IOException e) {
start = -1;
end = -1;
} finally {
if (theSock != null) {
try {
theSock.close();
} catch (IOException e) {}
}
if ((start != -1) && (end != -1)) {
total = end - start;
}
}
return total; //returns -1 if timeout
}
public static void main(String[] args) {
int port = 10000;
int timeout = 1000;
byte[] addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)1};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)5};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)6};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)7};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)8};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)9};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)10};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)11};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)12};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)5};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)6};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)7};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)8};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)9};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)10};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)11};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)12};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)20};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)21};
System.out.println(Ping2.testConn(addr, port, timeout));
addr = new byte[]{(byte)172,(byte)16,(byte)16,(byte)22};
System.out.println(Ping2.testConn(addr, port, timeout));
}
}