Here you can find the source of isPortFree(int port)
Parameter | Description |
---|---|
port | the port number to check |
public static boolean isPortFree(int port)
//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; } }