Java HTTP Port Find getAvailablePort()

Here you can find the source of getAvailablePort()

Description

get Available Port

License

Open Source License

Declaration

public static int getAvailablePort() 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j  a v  a 2s  .c  o  m*/
 * Copyright (c) 2015 Youssuf ElKalay / BuildScientist. 
 * All Rights Reserved.
 * This software is released under the Apache license 2.0
 * This file has been modified by the copyright holder.
 */

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

public class Main {
    public static int getAvailablePort() {
        ServerSocket serverside = null;
        int port = 0;
        try {
            serverside = new ServerSocket(0);
            port = serverside.getLocalPort();
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            try {
                serverside.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return port;
    }
}

Related

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