Java HTTP Port Find getFreePort()

Here you can find the source of getFreePort()

Description

get Free Port

License

Open Source License

Declaration

public static int getFreePort() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 EclipseSource 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  ww. j a v  a2s . c  o  m*/
 *    EclipseSource - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static int getFreePort() {
        int result;
        try {
            ServerSocket socket = new ServerSocket(0);
            result = socket.getLocalPort();
            socket.close();
        } catch (IOException ioe) {
            throw new RuntimeException("Failed to find free port.", ioe);
        }
        return result;
    }
}

Related

  1. getFreePort()
  2. getFreePort()
  3. getFreePort()
  4. getFreePort()
  5. getFreePort()
  6. getFreePort()
  7. getFreePort()
  8. getFreePort()
  9. getFreePort()