Java HTTP Port Find findFreePort()

Here you can find the source of findFreePort()

Description

Find a free port for communication with Prolog.

License

Open Source License

Exception

Parameter Description
IOException an exception

Return

free port number

Declaration

public static int findFreePort() throws IOException 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of the Prolog Development Tool (PDT)
 * // ww w . ja  va2 s.c o  m
 * Author: Lukas Degener (among others)
 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
 * Mail: pdt@lists.iai.uni-bonn.de
 * Copyright (C): 2004-2012, CS Dept. III, University of Bonn
 * 
 * All rights reserved. This program is  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
 * 
 ****************************************************************************/

import java.io.IOException;

import java.net.ServerSocket;

public class Main {
    /**
     * Find a free port for communication with Prolog.
     * 
     * @return free port number
     * @throws IOException
     */
    public static int findFreePort() throws IOException {
        ServerSocket ss = new ServerSocket(0);
        int port = ss.getLocalPort();
        ss.close();
        return port;
    }
}

Related

  1. findAvailablePorts(int n)
  2. findFreePort()
  3. findFreePort()
  4. findFreePort()
  5. findFreePort()
  6. findFreePort()
  7. findFreePort()
  8. findFreePort()
  9. findFreePort()