本文整理了Java中java.net.ServerSocket.getLocalPort()
方法的一些代码示例,展示了ServerSocket.getLocalPort()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServerSocket.getLocalPort()
方法的具体详情如下:
包路径:java.net.ServerSocket
类名称:ServerSocket
方法名:getLocalPort
[英]Gets the local port of this server socket or -1 if the socket is unbound.
[中]获取此服务器套接字的本地端口,如果套接字未绑定,则获取-1。
代码示例来源:origin: apache/incubator-dubbo
public static int getAvailablePort() {
try (ServerSocket ss = new ServerSocket()) {
ss.bind(null);
return ss.getLocalPort();
} catch (IOException e) {
return getRandomPort();
}
}
代码示例来源:origin: apache/zeppelin
public static int findRandomAvailablePortOnAllLocalInterfaces() throws IOException {
int port;
try (ServerSocket socket = new ServerSocket(0);) {
port = socket.getLocalPort();
socket.close();
}
return port;
}
代码示例来源:origin: Alluxio/alluxio
/** This method will close the socket upon first initialization. */
protected InetSocketAddress getWebAddressFromBindSocket() throws IOException {
Preconditions.checkNotNull(mWebBindSocket, "mWebBindSocket");
InetSocketAddress socketAddr = new InetSocketAddress(mWebBindSocket.getInetAddress(),
mWebBindSocket.getLocalPort());
mWebBindSocket.close();
return socketAddr;
}
代码示例来源:origin: alibaba/jstorm
/**
* Check whether the port is available to bind
*
* @param port port
* @return -1 means unavailable, otherwise available
* @throws IOException
*/
public static int tryPort(int port) throws IOException {
ServerSocket socket = new ServerSocket(port);
int rtn = socket.getLocalPort();
socket.close();
return rtn;
}
代码示例来源:origin: apache/incubator-dubbo
public static int getAvailablePort() {
try (ServerSocket ss = new ServerSocket()) {
ss.bind(null);
return ss.getLocalPort();
} catch (IOException e) {
return getRandomPort();
}
}
代码示例来源:origin: apache/geode
/**
* binds the server socket and gets threads going
*/
private void startAcceptor() throws ConnectionException {
int localPort;
int p = this.port;
createServerSocket();
try {
localPort = socket.getLocalPort();
id = new InetSocketAddress(socket.getInetAddress(), localPort);
stopped = false;
thread = new LoggingThread("P2P Listener Thread " + id, this);
try {
thread.setPriority(Thread.MAX_PRIORITY);
} catch (Exception e) {
logger.info("unable to set listener priority: {}", e.getMessage());
}
if (!Boolean.getBoolean("p2p.test.inhibitAcceptor")) {
thread.start();
} else {
logger.fatal(
"p2p.test.inhibitAcceptor was found to be set, inhibiting incoming tcp/ip connections");
socket.close();
}
} catch (IOException io) {
String s = "While creating ServerSocket on port " + p;
throw new ConnectionException(s, io);
}
this.port = localPort;
}
代码示例来源:origin: apache/hive
/**
* Finds a free port on the machine.
*
* @return
* @throws IOException
*/
public static int findFreePort() throws IOException {
ServerSocket socket= new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
return port;
}
代码示例来源:origin: stackoverflow.com
ServerSocket s = new ServerSocket(0);
System.out.println("listening on port: " + s.getLocalPort());
代码示例来源:origin: apache/hive
public static int findFreePort() throws IOException {
ServerSocket socket= new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
return port;
}
}
代码示例来源:origin: OryxProject/oryx
/**
* Binds to a free ephemeral port, and then releases it. The returned port is quite likely
* to be free for use after this, but is not entirely guaranteed to be.
*
* @return a (probably) free ephemeral port
* @throws IOException if an error occurs while binding to a port
*/
public static int chooseFreePort() throws IOException {
try (ServerSocket socket = new ServerSocket(0, 0)) {
return socket.getLocalPort();
}
}
代码示例来源:origin: alibaba/mdrill
/**
* ԴãжǷض˿
*
* @param port
* @return
* @throws IOException
*/
public static int try_port(int port) throws IOException {
ServerSocket socket = new ServerSocket(port);
int rtn = socket.getLocalPort();
socket.close();
return rtn;
}
代码示例来源:origin: SonarSource/sonarqube
int getAvailable(InetAddress address) {
try (ServerSocket socket = new ServerSocket(0, 50, address)) {
return socket.getLocalPort();
} catch (IOException e) {
throw new IllegalStateException("Fail to find an available port on " + address, e);
}
}
}
代码示例来源:origin: btraceio/btrace
private static int findFreePort() {
ServerSocket server = null;
int port = 0;
try {
server = new ServerSocket(0);
port = server.getLocalPort();
} catch (IOException e) {
port = DEFAULT_PORT;
} finally {
try {
server.close();
} catch (Exception e) {
// ignore
}
}
return port;
}
}
代码示例来源:origin: apache/incubator-gobblin
@Override
public int random() throws Exception {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}
代码示例来源:origin: jamesdbloom/mockserver
public static int findFreePort() {
int port;
try {
ServerSocket server = new ServerSocket(0);
port = server.getLocalPort();
server.close();
// allow time for the socket to be released
TimeUnit.MILLISECONDS.sleep(250);
} catch (Exception e) {
throw new RuntimeException("Exception while trying to find a free port", e);
}
return port;
}
}
代码示例来源:origin: apache/incubator-gobblin
@Override
public int specific(int port) throws Exception {
try (ServerSocket serverSocket = new ServerSocket(port)) {
return serverSocket.getLocalPort();
}
}
}
代码示例来源:origin: apache/hive
/**
* Finds a free port on the machine, but allow the
* ability to specify a port number to not use, no matter what.
*/
public static int findFreePortExcepting(int portToExclude) throws IOException {
ServerSocket socket1 = null;
ServerSocket socket2 = null;
try {
socket1 = new ServerSocket(0);
socket2 = new ServerSocket(0);
if (socket1.getLocalPort() != portToExclude) {
return socket1.getLocalPort();
}
// If we're here, then socket1.getLocalPort was the port to exclude
// Since both sockets were open together at a point in time, we're
// guaranteed that socket2.getLocalPort() is not the same.
return socket2.getLocalPort();
} finally {
if (socket1 != null){
socket1.close();
}
if (socket2 != null){
socket2.close();
}
}
}
代码示例来源:origin: go-lang-plugin-org/go-lang-idea-plugin
private static int findFreePort() {
try(ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
return socket.getLocalPort();
}
catch (Exception ignore) {
}
throw new IllegalStateException("Could not find a free TCP/IP port to start dlv");
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
/**
* Return a free port number. There is no guarantee it will remain free, so
* it should be used immediately.
*
* @returns A free port for binding a local socket
*/
public static int getFreeSocketPort() {
int port = 0;
try {
ServerSocket s = new ServerSocket(0);
port = s.getLocalPort();
s.close();
return port;
} catch (IOException e) {
// Could not get a free port. Return default port 0.
}
return port;
}
代码示例来源:origin: apache/flink
/**
* Find a non-occupied port.
*
* @return A non-occupied port.
*/
public static int getAvailablePort() {
for (int i = 0; i < 50; i++) {
try (ServerSocket serverSocket = new ServerSocket(0)) {
int port = serverSocket.getLocalPort();
if (port != 0) {
return port;
}
}
catch (IOException ignored) {}
}
throw new RuntimeException("Could not find a free permitted port on the machine.");
}
内容来源于网络,如有侵权,请联系作者删除!