Here you can find the source of isPortInUse(InetAddress address, int port, int count)
Parameter | Description |
---|---|
address | a local InetAddress |
port | the port number to check |
count | the number of times to retry |
true
if the port is in use, and false
otherwise
public static boolean isPortInUse(InetAddress address, int port, int count)
//package com.java2s; /******************************************************************************* * Copyright (c) 2003, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w w w . j a va 2 s . c om*/ * IBM Corporation - Initial API and implementation *******************************************************************************/ import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.SocketException; public class Main { /** * Checks to see if the given local port number is being used. * Returns <code>true</code> if the given port is in use, and <code>false</code> * otherwise. Retries every 500ms for "count" tries. * * @param port the port number to check * @param count the number of times to retry * @return boolean <code>true</code> if the port is in use, and * <code>false</code> otherwise */ public static boolean isPortInUse(int port, int count) { return isPortInUse(null, port, count); } /** * Checks to see if the given local port number is being used. * Returns <code>true</code> if the given port is in use, and <code>false</code> * otherwise. Retries every 500ms for "count" tries. * * @param address a local InetAddress * @param port the port number to check * @param count the number of times to retry * @return boolean <code>true</code> if the port is in use, and * <code>false</code> otherwise * @since 1.1 */ public static boolean isPortInUse(InetAddress address, int port, int count) { boolean inUse = isPortInUse(address, port); while (inUse && count > 0) { try { Thread.sleep(500); } catch (Exception e) { // ignore } inUse = isPortInUse(address, port); count--; } return inUse; } /** * Checks to see if the given local port number is being used. * Returns <code>true</code> if the given port is in use, and <code>false</code> * otherwise. * * @param port the port number to check * @return boolean <code>true</code> if the port is in use, and * <code>false</code> otherwise */ public static boolean isPortInUse(int port) { return isPortInUse(null, port); } /** * Checks to see if the given local port number is being used. * Returns <code>true</code> if the given port is in use, and <code>false</code> * otherwise. * * @param address a local InetAddress * @param port the port number to check * @return boolean <code>true</code> if the port is in use, and * <code>false</code> otherwise * @since 1.1 */ public static boolean isPortInUse(InetAddress address, int port) { ServerSocket s = null; try { s = new ServerSocket(port, 0, address); } catch (SocketException e) { return true; } catch (IOException e) { return true; } catch (Exception e) { return true; } finally { if (s != null) { try { s.close(); } catch (Exception e) { // ignore } } } return false; } }