bozer
asked on
Test Server connectivity/availability with Java code
Hello experts,
I would like to have a very quick check on the connectivity/availability of a server where an IIS is installed.
I managed to use ping with 'Runtime' but that is not exactly what I want. Even if I parse the result where there's no packet lost, sending 4 packets is still taking too long.
What is a good (and quick) way to do this in java? Please provide a sample code too.
Thank you in advance!
I would like to have a very quick check on the connectivity/availability of a server where an IIS is installed.
I managed to use ping with 'Runtime' but that is not exactly what I want. Even if I parse the result where there's no packet lost, sending 4 packets is still taking too long.
What is a good (and quick) way to do this in java? Please provide a sample code too.
Thank you in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.velocityreviews.com/forums/t146589-ping-class-java.html
//this class is for testing the conectivity to a database
//actually it times the connection setup time
//in theory this can be used to find the closest database to the client
location.
//date may-09-2004
package Services;
import java.io.*;
import java.net.*;
public class Pinger {
// byte[] addr1 = new byte[]{(byte)192,(byte)168,(byte)2,(byte)5};
public static long testDBConn(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 = new Socket();
try {
InetAddress addr = InetAddress.getByAddress(addr1);
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
start = System.currentTimeMillis();
theSock.connect(sockaddr, timeoutMs);
end = System.currentTimeMillis();
} 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
}
}
Reply With Quote
I think you can use the above code with port 80, as you have http server
running on this host
running on this host
ASKER
Thank you
http://stackoverflow.com/questions/2448666/how-to-do-a-true-java-ping-from-windows