Java HTTP Port Find getRandomOpenPort()

Here you can find the source of getRandomOpenPort()

Description

get Random Open Port

License

Open Source License

Declaration

public static int getRandomOpenPort() throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Red Hat, Inc. //w  w w .  jav  a  2s.  c o m
 * 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:
 *        Red Hat Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/

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

public class Main {
    public static int getRandomOpenPort() throws IOException {
        try (ServerSocket socket = new ServerSocket(0)) {
            return socket.getLocalPort();
        }
    }
}

Related

  1. getPorts(int number, int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)
  2. getPrometheusMetrics(int metricsPort)
  3. getRandomAvailablePort()
  4. getRandomFreePort()
  5. getRandomFreePort()
  6. getRandomPort()
  7. getRandomPort()
  8. getRandomPort()
  9. getRandomPorts(int n)