Java HTTP Port Find findAvailablePort()

Here you can find the source of findAvailablePort()

Description

find Available Port

License

Apache License

Declaration

protected static int findAvailablePort() 

Method Source Code

//package com.java2s;
/*//from  w ww  . j av a  2s .co 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.net.ServerSocket;

public class Main {
    protected static int findAvailablePort() {
        try {
            ServerSocket ss = new ServerSocket(0);
            int result = ss.getLocalPort();
            ss.close();
            return result;
        } catch (Exception e) {
            return 5555;
        }
    }
}

Related

  1. canConnectOn(String host, int port)
  2. createUnresolved(String host, int port)
  3. doSend(String command, String server, String port)
  4. executeHttpCommand(String host, int port, String request, String charset)
  5. exportResource(Class fromClass, String resourceName, String exportPath)
  6. findAvailablePort()
  7. findAvailablePort()
  8. findAvailablePort()
  9. findAvailablePort()