Java HTTP Port Find findFreePort()

Here you can find the source of findFreePort()

Description

Attempts to find a free port

License

Apache License

Exception

Parameter Description
IOException an exception

Declaration

public static int findFreePort() throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w ww.j  a v  a 2 s .  c om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.IOException;

import java.net.ServerSocket;

public class Main {
    private static final int DEFAULT_PORT = 12321;

    /**
     * Attempts to find a free port
     * 
     * @throws IOException
     * 
     * @throws IOException
     */
    public static int findFreePort() throws IOException {
        return findFreePort(DEFAULT_PORT);
    }

    /**
     * Attempts to find a free port
     * @param initPort The first port to try, before resolving to 
     *   brute force searching
     * @throws IOException
     * 
     * @throws IOException
     */
    public static int findFreePort(int initPort) throws IOException {
        int port = -1;
        ServerSocket tmpSocket = null;
        // first try the default port
        try {
            tmpSocket = new ServerSocket(initPort);

            port = initPort;

            System.out.println("Using default port: " + port);
        } catch (IOException e) {
            System.out.println("Failed to use specified port");
            // didn't work, try to find one dynamically
            try {
                int attempts = 0;

                while (port < 1024 && attempts < 2000) {
                    attempts++;

                    tmpSocket = new ServerSocket(0);

                    port = tmpSocket.getLocalPort();
                }

            } catch (IOException e1) {
                throw new IOException("Failed to find a port to use for testing: " + e1.getMessage());
            }
        } finally {
            if (tmpSocket != null) {
                try {
                    tmpSocket.close();
                } catch (IOException e) {
                    // ignore
                }
                tmpSocket = null;
            }
        }

        return port;
    }
}

Related

  1. findFreePort()
  2. findFreePort()
  3. findFreePort()
  4. findFreePort()
  5. findFreePort()
  6. findFreePort()
  7. findFreePort(int start, int len)
  8. findFreePort(int start, int limit)
  9. findFreePort(int startPort)