Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding two free tcp ports

Tags:

java

tcp

sockets

I understand that the following code can (perhaps not very efficiently) find out a free TCP port in Java:

  public static int findFreePort() {
    int port;
    try {
      ServerSocket socket= new ServerSocket(0);
      port = socket.getLocalPort();
      socket.close(); 
    } catch (Exception e) { port = -1; }
    return port;    
  } 

(There are some related questions here in SO - for example).

What I don't understand is why (or whether) two succesive calls to this method are guaranteed to return two different ports. This is assumed, for example, here (search for the calls to findFreePort method).

Is this correct?

like image 215
leonbloy Avatar asked Jul 16 '10 14:07

leonbloy


People also ask

Which TCP ports are free to use?

Ports 49152-65535– These are used by client programs and you are free to use these in client programs. When a Web browser connects to a web server the browser will allocate itself a port in this range. Also known as ephemeral ports.

How do I find unused TCP ports?

You can use "netstat" to check whether a port is available or not. Use the netstat -anp | find "port number" command to find whether a port is occupied by an another process or not. If it is occupied by an another process, it will show the process id of that process.


2 Answers

In the Javadoc specification, I don't see any line saying that two succesive calls is guaranteed to return two different ports...

Since the ServerSocket is closed, a second call could give the same port. Statistically, it is unprobable but not impossible I think.

If you open your two ServerSocket, get the ports, and then, close your two ServerSocket, you are guarranted to get two different ports (since the first is not free when you create the second ServerSocket).

Example method to get n different free ports :

public int[] getFreePorts(int portNumber) throws IOException {
    int[] result = new int[portNumber];
    List<ServerSocket> servers = new ArrayList<ServerSocket>(portNumber);
    ServerSocket tempServer = null;

    for (int i=0; i<portNumber; i++) {
        try {
            tempServer = new ServerSocket(0);
            servers.add(tempServer);
            result[i] = tempServer.getLocalPort();
        } finally {
            for (ServerSocket server : servers) {
                try {
                    server.close();
                } catch (IOException e) {
                    // Continue closing servers.
                }
            }
        }
    }
    return result;
}
like image 140
Benoit Courtine Avatar answered Oct 13 '22 10:10

Benoit Courtine


One way to get two different port numbers:

  ServerSocket socket1 = new ServerSocket(0);
  port1 = socket1.getLocalPort();
  ServerSocket socket2 = new ServerSocket(0);
  port2 = socket2.getLocalPort();

  socket1.close();
  socket2.close(); 
like image 42
Noel M Avatar answered Oct 13 '22 11:10

Noel M