Java HTTP Port Find getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)

Here you can find the source of getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)

Description

get Ports

License

Apache License

Declaration

public static List<Integer> getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w .ja v a  2  s.  c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Integer> getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER) {
        int start = MIN_PORT_NUMBER + 1;
        boolean stop = false;
        List<Integer> result = new ArrayList<Integer>();
        while (!stop && start < MAX_PORT_NUMBER) {
            if (available(start, MIN_PORT_NUMBER, MAX_PORT_NUMBER)) {
                result.add(start);
                if (result.size() == number) {
                    stop = true;
                }
                start = start + 1;
            }
        }
        if (result.size() != number) {
            throw new IllegalArgumentException(
                    "Can not collect enough port in range " + MIN_PORT_NUMBER + " and " + MAX_PORT_NUMBER);
        }
        return result;
    }

    public static boolean available(int port, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER) {
        if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
            throw new IllegalArgumentException("Invalid start port: " + port);
        }

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    /* should not be thrown */
                }
            }
        }

        return false;
    }
}

Related

  1. getPort(Proxy proxy)
  2. getPort(String endpoint)
  3. getPort(String hostAndPort)
  4. getPortInRange(int minPort, int maxPort)
  5. getPorts(int count)
  6. getPrometheusMetrics(int metricsPort)
  7. getRandomAvailablePort()
  8. getRandomFreePort()
  9. getRandomFreePort()