博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FreePortScanner.java
阅读量:6302 次
发布时间:2019-06-22

本文共 2884 字,大约阅读时间需要 9 分钟。

/* * Copyright 2002-2010 the original author or authors. * * Licensed 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. */package org.springframework.http.client;import java.io.IOException;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.util.Random;import org.springframework.util.Assert;/** * Utility class that finds free BSD ports for use in testing scenario's. * * @author Ben Hale * @author Arjen Poutsma */public abstract class FreePortScanner {    private static final int MIN_SAFE_PORT = 1024;    private static final int MAX_PORT = 65535;    private static final Random random = new Random();    /**     * Returns the number of a free port in the default range.     */    public static int getFreePort() {        return getFreePort(MIN_SAFE_PORT, MAX_PORT);    }    /**     * Returns the number of a free port in the given range.     */    public static int getFreePort(int minPort, int maxPort) {        Assert.isTrue(minPort > 0, "'minPort' must be larger than 0");        Assert.isTrue(maxPort > minPort, "'maxPort' must be larger than minPort");        int portRange = maxPort - minPort;        int candidatePort;        int searchCounter = 0;        do {            if (++searchCounter > portRange) {                throw new IllegalStateException(                        String.format("There were no ports available in the range %d to %d", minPort, maxPort));            }            candidatePort = getRandomPort(minPort, portRange);        }        while (!isPortAvailable(candidatePort));        return candidatePort;    }    private static int getRandomPort(int minPort, int portRange) {        return minPort + random.nextInt(portRange);    }    private static boolean isPortAvailable(int port) {        ServerSocket serverSocket;        try {            serverSocket = new ServerSocket();        }        catch (IOException ex) {            throw new IllegalStateException("Unable to create ServerSocket.", ex);        }        try {            InetSocketAddress sa = new InetSocketAddress(port);            serverSocket.bind(sa);            return true;        }        catch (IOException ex) {            return false;        }        finally {            try {                serverSocket.close();            }            catch (IOException ex) {                // ignore            }        }    }}

 

转载于:https://www.cnblogs.com/xzs603/p/3170753.html

你可能感兴趣的文章
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>
Eclipse 添加 Ibator
查看>>
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
查看>>
Python编程语言
查看>>
十四、转到 linux
查看>>
Got error 241 'Invalid schema
查看>>
ReferenceError: event is not defined
查看>>
男人要内在美,更要外在美
查看>>
为什么要跟别人比?
查看>>
app启动白屏
查看>>
Oracle 提高查询性能(基础)
查看>>
学习知识应该像织网一样去学习——“网状学习法”
查看>>
Hadoop集群完全分布式安装
查看>>
QString,char,string之间赋值
查看>>
我的友情链接
查看>>
Nginx+mysql+php-fpm负载均衡配置实例
查看>>
shell脚本操作mysql数据库 (部份参考)
查看>>
MySql之基于ssl安全连接的主从复制
查看>>
informix的逻辑日志和物理日志分析
查看>>