Test Server connectivity/availability with Java code

Posted on 2011-05-02
Last Modified: 2012-05-11
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!
Question by:bozer
    LVL 92

    Accepted Solution

    LVL 92

    Expert Comment

    LVL 47

    Expert Comment


    //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
    //date may-09-2004
    package Services;
    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 {
    } catch (IOException e) {
    if ((start != -1) && (end != -1)) {
    total = end - start;
    return total; //returns -1 if timeout
    Reply With Quote

    Open in new window

    LVL 47

    Expert Comment

    I think you can use the above code with port 80, as you have http server
    running on this host

    Author Closing Comment

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    NotAlone Challenge 20 65
    MaxSpan challenge 9 50
    wordcount challenge 11 47
    firstChar challenge 13 56
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now