集册 Java实例教程 查找空闲端口

查找空闲端口

欢马劈雪     最近更新时间:2020-01-02 10:19:05

531
查找空闲端口
/**
 * N o w J a v a . c o m - 时  代  Java 提 供 
**/


//package com.nowjava;

import java.io.IOException;


import java.net.ServerSocket;


public class Main {

    public static int findFreePort() {

        try {

            ServerSocket server = new ServerSocket(0);

            int port = server.getLocalPort();

            server.close();

            return port;

        } catch (IOException e) {

            throw new RuntimeException(

                    
展开阅读全文