Java InetAddress findUnusedPort(InetAddress address, int low, int high)

Here you can find the source of findUnusedPort(InetAddress address, int low, int high)

Description

Finds an unused local port between the given from and to values.

License

Open Source License

Parameter

Parameter Description
address a local InetAddress
low lowest possible port number
high highest possible port number

Return

an unused port number, or -1 if no used ports could be found

Declaration

public static int findUnusedPort(InetAddress address, int low, int high) 

Method Source Code

//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://w w  w .j  a v a 2s  . co m
 *     IBM Corporation - Initial API and implementation
 *******************************************************************************/

import java.io.IOException;
import java.net.InetAddress;

import java.net.ServerSocket;
import java.net.SocketException;

import java.util.Random;

public class Main {
    private static final Random rand = new Random(System.currentTimeMillis());

    /**
     * Finds an unused local port between the given from and to values.
     * 
     * @param low lowest possible port number
     * @param high highest possible port number
     * @return an unused port number, or <code>-1</code> if no used ports could be found
     */
    public static int findUnusedPort(int low, int high) {
        return findUnusedPort(null, low, high);
    }

    /**
     * Finds an unused local port between the given from and to values.
     * 
     * @param address a local InetAddress
     * @param low lowest possible port number
     * @param high highest possible port number
     * @return an unused port number, or <code>-1</code> if no used ports could be found
     * @since 1.1
     */
    public static int findUnusedPort(InetAddress address, int low, int high) {
        if (high < low)
            return -1;

        for (int i = 0; i < 10; i++) {
            int port = getRandomPort(low, high);
            if (!isPortInUse(address, port))
                return port;
        }
        return -1;
    }

    /**
     * Return a random local port number in the given range.
     * 
     * @param low lowest possible port number
     * @param high highest possible port number
     * @return a random port number in the given range
     */
    private static int getRandomPort(int low, int high) {
        return rand.nextInt(high - low) + low;
    }

    /**
     * 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;
    }
}

Related

  1. createResolved(InetAddress address, int port)
  2. createSocket(InetAddress address, int port, boolean fixedPort)
  3. doMatch(InetAddress inetAddress, String toMatchParam)
  4. findFreePort(InetAddress address, int start, int end)
  5. findUnusedPort(InetAddress address, int from, int to)
  6. formatAddress(InetAddress address, int port, Context cx, Scriptable scope)
  7. formatAddress(InetAddress inet)
  8. formatHostAddress(final InetAddress localHost)
  9. greaterThan(InetAddress inetAddress1, InetAddress inetAddress2)