Java HTTP Port Find isPortFree(int port)

Here you can find the source of isPortFree(int port)

Description

Check and log is a given port is available

License

Open Source License

Parameter

Parameter Description
port the port number to check

Return

true if the port is available, false otherwise

Declaration

public static boolean isPortFree(int port) 

Method Source Code


//package com.java2s;
/*/*www  . j  av  a  2s  . co  m*/
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.IOException;
import java.net.ServerSocket;

public class Main {
    /**
     * Check and log is a given port is available
     * 
     * @param port the port number to check
     * @return true if the port is available, false otherwise
     */
    public static boolean isPortFree(int port) {
        boolean portIsFree = true;

        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            portIsFree = false;
        } finally {
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
        return portIsFree;
    }
}

Related

  1. isPortAvailable(String hostname, int port)
  2. isPortAvailable(String hostname, int port)
  3. isPortAvailable(String portNumber)
  4. isPortBound(int port)
  5. isPortFree(int port)
  6. isPortFree(int portNumber)
  7. isPortFree(String hostName, String port)
  8. isPortFreeClient(String hostName, int portNumber)
  9. isPortInUse(final String host, final Integer port)