Java InetAddress Create getNextAliablePort(InetAddress address, int startport)

Here you can find the source of getNextAliablePort(InetAddress address, int startport)

Description

get Next Aliable Port

License

Open Source License

Declaration

static public int getNextAliablePort(InetAddress address, int startport) 

Method Source Code


//package com.java2s;
import java.net.*;

public class Main {
    static public int getNextAliablePort(String address, int startport) throws UnknownHostException {
        InetAddress a = InetAddress.getByName(address);
        return getNextAliablePort(a, startport);
    }//from   w  w  w.  ja v  a 2 s.  c om

    static public int getNextAliablePort(InetAddress address, int startport) {
        int port = startport;
        while (port < 65535) {
            try {
                Socket socket = new Socket(address, port);
                socket.close();

                port++;
            } catch (Exception ex) {
                return port;
            }
        }
        return -1;
    }
}

Related

  1. getNetwork(InetAddress start, InetAddress end)
  2. getNetworkCIDR(InetAddress addr, short prefixLength)
  3. getNetworkInterface(InetAddress localAddress)
  4. getNetworkInterfaceByIp(InetAddress inetAddress)
  5. getNetworkInterfaceForRemoteIPAddress( InetAddress remoteAddress)
  6. getNextAvailablePort(InetAddress address, int port)
  7. getOneDotPrefix(InetAddress addr)
  8. getPing(InetAddress address, long timeout)
  9. getPrivateInetInetAddress()