Sockets: Discover port availability using Java
Asked Answered
A

10

147

How do I programmatically determine the availability of a port in a given machine using Java?

i.e given a port number, determine whether it is already being used or not?.

Allonym answered 12/1, 2009 at 7:38 Comment(2)
While this question is about how to know if a given port is already in use, you may have landed here trying to find a way to obtain a free port number, which #2675862 (with link to my gist.github.com/3429822) covers better.Assibilate
For what purpose? If you just want to find a vacant socket to listen at, just specify zero. Any scanning technique is liable to timing-window problems: the port can be vacant when you scan and occupied when you go to claim in.Celebrated
C
100

This is the implementation coming from the Apache camel project:

/**
 * Checks to see if a specific port is available.
 *
 * @param port the port to check for availability
 */
public static boolean available(int port) {
    if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
        throw new IllegalArgumentException("Invalid start port: " + 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;
}

They are checking the DatagramSocket as well to check if the port is avaliable in UDP and TCP.

Compel answered 12/1, 2009 at 14:31 Comment(8)
A nice variant of this if you have MINA is AvailablePortFinder.getNextAvailable(1024) which gets you the next non-privileged port.Erroneous
This doesn't catch everything however. I've been bitten by a running nginx on TCP 8000 whilst the above routine reports 8000 as available. No idea why -- I suspect nginx does some sneaky stuff (this is on OS X). Workaround for me is to do the above and also to open a new Socket("localhost", port) then return false if we don't get an exception. Thoughts?Revelation
I've had the same issue with OS X.Fixative
Same issue on Windows trying to detect if papercut SMTP server is running. A solution where you open a connection to a port, rather than trying to bind to a it (as suggested by Partly Clodys comment and Ivan's answer) seems to work more reliable.Distilled
Interesting. The call to setReuseAddress() is completely pointless after the socket is already bound, and it it also completely contrary to the purpose of the code.Celebrated
without setReuseAddress the port would not be free to use for some time after closing the socket. Actually its a system call that gets called after bounding a socketNortherner
@pendrive Actually it is a system call that should be called before binding a socket. Calling it afterwards is a complete waste of time.Celebrated
THis code is subject to timing-window problems. If the objective is to create a ServerSocket listening to some port, that's what it should do, and return the ServerSocket. Creating it and then closing it and returning it provides zero guarantee that a subsequent ServerSocket can be created using that port. Ditto for DatagramSocket.Celebrated
S
48

For Java 7 you can use try-with-resource for more compact code:

private static boolean available(int port) throws IllegalStateException {
    try (Socket ignored = new Socket("localhost", port)) {
        return false;
    } catch (ConnectException e) {
        return true;
    } catch (IOException e) {
        throw new IllegalStateException("Error while trying to check open port", e);
    }
}
Stew answered 11/3, 2013 at 13:50 Comment(5)
This doesn't test whether at port is available. It tests whether it is in LISTEN state, whether the IP address is reachable, etc.Celebrated
Additionally this test is pretty slow (nearly a second per port).Atomic
shouldn't it return false when an IOException is caught?Cattail
@BaroudiSafwen It depends entirely on what the exception actually is. For ConnectException: 'connection refused', yes it should return false. For timeouts, nothing it could return would be valid, as the actual answer isn't known. That's why this technique is useless for this purpose.Celebrated
@BaroudiSafwen if the port you passed is being used, it will not throw an exception, returning false from the body of the try block. In contrast, if the port is not in use, it will throw an exception, in which case, you want the method to return true. However, as JMax commented, this test is slow.Scapegoat
K
42

It appears that as of Java 7, David Santamaria's answer doesn't work reliably any more. It looks like you can still reliably use a Socket to test the connection, however.

private static boolean available(int port) {
    System.out.println("--------------Testing port " + port);
    Socket s = null;
    try {
        s = new Socket("localhost", port);

        // If the code makes it this far without an exception it means
        // something is using the port and has responded.
        System.out.println("--------------Port " + port + " is not available");
        return false;
    } catch (IOException e) {
        System.out.println("--------------Port " + port + " is available");
        return true;
    } finally {
        if( s != null){
            try {
                s.close();
            } catch (IOException e) {
                throw new RuntimeException("You should handle this error." , e);
            }
        }
    }
}
Koloski answered 11/12, 2012 at 18:21 Comment(3)
I want to check if a proxy server is available to makes call on his behalf.Eclair
@DavidSantamaria's answer never worked. Nothing to do with Java 7.Celebrated
if it is not available, then will it goes to finally statement ? because it's already returned from your code...?Petrochemical
G
38

If you're not too concerned with performance, you could always try listening on a port using the ServerSocket class. If it throws an exception odds are it's being used.

public static boolean isAvailable(int portNr) {
  boolean portFree;
  try (var ignored = new ServerSocket(portNr)) {
      portFree = true;
  } catch (IOException e) {
      portFree = false;
  }
  return portFree;
}

EDIT: If all you're trying to do is select a free port then new ServerSocket(0) will find one for you.

Gondola answered 12/1, 2009 at 7:47 Comment(11)
Use finally for cleanup (try { ... } catch { ... } finally { if (socket != null) socket.close(); }Burgener
You can also set "portTaken = (socket == null);" at the end instead of doing it in the catch.Burgener
This attempts to bind to the socket on all (0.0.0.0) interfaces. You can use explicit addresses to check specific ports on specific addresses. see java.sun.com/j2se/1.4.2/docs/api/java/net/…Passe
I didn't feel that fell within the intended scope of the author's question.Gondola
A minor mistake that an initialization should be: ServerSocket serverSocket = null;Monkish
Is there a way to do it without try/catch? I wouldn't mind using any available port for my purposes, but iterating over port numbers and try/catch-ing over each one until I find a free one is a little wasteful. Isn't there a 'native boolean available(int)' method somewhere, which just checks?Sox
new SeverSocket(0) will automatically select a free port .Gondola
I have the same requirement as Oz. However, ServerSocket(0) sounds like a good second-best, so I've upvoted both these comments. I think ServerSocket(0) is worth putting as a separate full answer in fact.Spirelet
@HughPerkins It is a much better first-best.Celebrated
@Hosam Aly: Socket implements the Closable interface. The try-catch-finally (with ressource) will call the socket.close() for you.Siusiubhan
@primehunter: that wasn't an option in 2009 :)Burgener
A
15

The following solution is inspired by the SocketUtils implementation of Spring-core (Apache license).

Compared to other solutions using Socket(...) it is pretty fast (testing 1000 TCP ports in less than a second).

Of course it can only detect services that open a port on all interfaces or that explicitly run on localhost.

public static boolean isTcpPortAvailable(int port) {
    try (ServerSocket serverSocket = new ServerSocket()) {
        // setReuseAddress(false) is required only on macOS, 
        // otherwise the code will not work correctly on that platform          
        serverSocket.setReuseAddress(false);
        serverSocket.bind(new InetSocketAddress(InetAddress.getByName("localhost"), port), 1);
        return true;
    } catch (Exception ex) {
        return false;
    }
}       
Atomic answered 16/2, 2018 at 13:56 Comment(1)
Thanks, worked for me on macOS 11 (Big Sur)Otherworldly
P
6

A cleanup of the answer pointed out by David Santamaria:

/**
 * Check to see if a port is available.
 *
 * @param port
 *            the port to check for availability.
 */
public static boolean isPortAvailable(int port) {
    try (var ss = new ServerSocket(port); var ds = new DatagramSocket(port)) {
        return true;
    } catch (IOException e) {
        return false;
    }
}

This is still subject to a race condition pointed out by user207421 in the comments to David Santamaria's answer (something could grab the port after this method closes the ServerSocket and DatagramSocket and returns).

Presser answered 8/3, 2019 at 5:43 Comment(2)
If u dont close the socket, wont u occupy the socket although the reference is getting cleared?Mercenary
The socket does get closed here -- this is a try-with-resources block.Presser
W
4

The try/catch socket based solutions , might not yield accurate results (the socket address is "localhost" and in some cases the port could be "occupied" not by the loopback interface and at least on Windows I've seen this test fails i.e. the prot falsely declared as available).

There is a cool library named SIGAR , the following code can hook you up :

Sigar sigar = new Sigar();
int flags = NetFlags.CONN_TCP | NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT;             NetConnection[] netConnectionList = sigar.getNetConnectionList(flags);
for (NetConnection netConnection : netConnectionList) {
   if ( netConnection.getLocalPort() == port )
        return false;
}
return true;
Washbowl answered 12/3, 2013 at 18:34 Comment(2)
I get: "no sigar-x86-winnt.dll in java.library.path" Unfortunately it requires download of some additional dll that is not really viable in enterprise environment (I have no control over CI system). So bad.... Thanks anyway.Attired
@Attired I guess you still have control on your app deployment package, if this is the case you can always provide Sigar native runtime DLL/SOs near your JARs and set the JAVA lib path pragmatically (via simple hack you can influence it even after your app has launched by the JVM).Washbowl
P
1

In my case it helped to try and connect to the port - if service is already present, it would respond.

    try {
        log.debug("{}: Checking if port open by trying to connect as a client", portNumber);
        Socket sock = new Socket("localhost", portNumber);          
        sock.close();
        log.debug("{}: Someone responding on port - seems not open", portNumber);
        return false;
    } catch (Exception e) {         
        if (e.getMessage().contains("refused")) {
            return true;
    }
        log.error("Troubles checking if port is open", e);
        throw new RuntimeException(e);              
    }
Paradrop answered 31/5, 2012 at 5:52 Comment(1)
Not what was asked for.Celebrated
S
0

In my case I had to use DatagramSocket class.

boolean isPortOccupied(int port) {
    DatagramSocket sock = null;
    try {
        sock = new DatagramSocket(port);
        sock.close();
        return false;
    } catch (BindException ignored) {
        return true;
    } catch (SocketException ex) {
        System.out.println(ex);
        return true;
    }
}

Don't forget to import first

import java.net.DatagramSocket;
import java.net.BindException;
import java.net.SocketException;
Suitcase answered 6/11, 2017 at 13:46 Comment(1)
This works for UDP ports. The question appears to be about TCP ports.Celebrated
A
-2

I have Tried something Like this and it worked really fine with me

            Socket Skt;
            String host = "localhost";
            int i = 8983; // port no.

                 try {
                    System.out.println("Looking for "+ i);
                    Skt = new Socket(host, i);
                    System.out.println("There is a Server on port "
                    + i + " of " + host);
                 }
                 catch (UnknownHostException e) {
                    System.out.println("Exception occured"+ e);

                 }
                 catch (IOException e) {
                     System.out.println("port is not used");

                 }
Aksoyn answered 28/10, 2015 at 6:56 Comment(2)
Not what was asked for.Celebrated
And also a socket leak.Celebrated

© 2022 - 2024 — McMap. All rights reserved.