Matthew
Matthew

Reputation: 11280

Detecting port availability in Java fails

I try to check if port 80 is available using the following method :

Sockets: Discover port availability using Java

I have a Java application that checks if port 80 is available, if so, it runs a small web server listening on port 80. It works great to detect if another Java application listens on port 80, e.g. if I run my application two times, the second instance will correctly tell me that the port 80 is being used.

The problem is that I have WAMP running and listening on port 80, and that if I run my Java application after I started WAMP, it won't tell me that the port 80 is busy. It seems that it only tells me if another Java application uses the port 80.

That goes beyond my understanding ... any help is greatly appreciated!

Code snippet:

int port = 80;
if(!Connection.isPortAvailable(port)) {
    logger.info("Port " + port + " is already in use");
}
// in Connection class
public static boolean isPortAvailable(int port) {
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        ds = new DatagramSocket(port);
        ds.setReuseAddress(true);
        return true;
    } catch (IOException e) {
    } finally {
        if (ds != null) {
            ds.close();
        }

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
                /* should not be thrown */
            }
        }
    }

    return false;
}

Upvotes: 2

Views: 938

Answers (2)

user207421
user207421

Reputation: 311050

The correct answer to all questions of this nature is to try to use it and catch the exception. Not try to see if it's available and then try to use it and still have to handle the exception, which has several obvious problems:

  1. There is a timing window between 'see' and 'try' during which the situation can change (both ways).
  2. You still have to catch failures in the 'use' part anyway.
  3. It is basically just trying to predict the future. This is supposed to be computer science, not fortune-telling.

This applies to most values of 'it', including network ports, files, any resource really.

Upvotes: 2

Kevin Tonon
Kevin Tonon

Reputation: 995

I was able to reproduce your problem by running WampServer (verified that it was running by visiting localhost:80) and running a minimal java program given your example code.

The code in the try block did not throw an exception when WampServer was running. However, modify the first few lines of the try block like this

ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", port));

and isPortAvailable will properly detect when WampServer is running and when it is not. Using "0.0.0.0" instead of "127.0.0.1" didn't work with WampServer, but did properly detect when IIS was running. You can check both by closing the first socket

ss = new ServerSocket();
ss.bind(new InetSocketAddress("0.0.0.0", port));
ss.close();

ss = new ServerSocket();
ss.bind(new InetSocketAddress("127.0.0.1", port));

Upvotes: 1

Related Questions