java.net.ServerSocket.getLocalPort()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(290)

本文整理了Java中java.net.ServerSocket.getLocalPort()方法的一些代码示例,展示了ServerSocket.getLocalPort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServerSocket.getLocalPort()方法的具体详情如下:
包路径:java.net.ServerSocket
类名称:ServerSocket
方法名:getLocalPort

ServerSocket.getLocalPort介绍

[英]Gets the local port of this server socket or -1 if the socket is unbound.
[中]获取此服务器套接字的本地端口,如果套接字未绑定,则获取-1。

代码示例

代码示例来源:origin: apache/incubator-dubbo

  1. public static int getAvailablePort() {
  2. try (ServerSocket ss = new ServerSocket()) {
  3. ss.bind(null);
  4. return ss.getLocalPort();
  5. } catch (IOException e) {
  6. return getRandomPort();
  7. }
  8. }

代码示例来源:origin: apache/zeppelin

  1. public static int findRandomAvailablePortOnAllLocalInterfaces() throws IOException {
  2. int port;
  3. try (ServerSocket socket = new ServerSocket(0);) {
  4. port = socket.getLocalPort();
  5. socket.close();
  6. }
  7. return port;
  8. }

代码示例来源:origin: Alluxio/alluxio

  1. /** This method will close the socket upon first initialization. */
  2. protected InetSocketAddress getWebAddressFromBindSocket() throws IOException {
  3. Preconditions.checkNotNull(mWebBindSocket, "mWebBindSocket");
  4. InetSocketAddress socketAddr = new InetSocketAddress(mWebBindSocket.getInetAddress(),
  5. mWebBindSocket.getLocalPort());
  6. mWebBindSocket.close();
  7. return socketAddr;
  8. }

代码示例来源:origin: alibaba/jstorm

  1. /**
  2. * Check whether the port is available to bind
  3. *
  4. * @param port port
  5. * @return -1 means unavailable, otherwise available
  6. * @throws IOException
  7. */
  8. public static int tryPort(int port) throws IOException {
  9. ServerSocket socket = new ServerSocket(port);
  10. int rtn = socket.getLocalPort();
  11. socket.close();
  12. return rtn;
  13. }

代码示例来源:origin: apache/incubator-dubbo

  1. public static int getAvailablePort() {
  2. try (ServerSocket ss = new ServerSocket()) {
  3. ss.bind(null);
  4. return ss.getLocalPort();
  5. } catch (IOException e) {
  6. return getRandomPort();
  7. }
  8. }

代码示例来源:origin: apache/geode

  1. /**
  2. * binds the server socket and gets threads going
  3. */
  4. private void startAcceptor() throws ConnectionException {
  5. int localPort;
  6. int p = this.port;
  7. createServerSocket();
  8. try {
  9. localPort = socket.getLocalPort();
  10. id = new InetSocketAddress(socket.getInetAddress(), localPort);
  11. stopped = false;
  12. thread = new LoggingThread("P2P Listener Thread " + id, this);
  13. try {
  14. thread.setPriority(Thread.MAX_PRIORITY);
  15. } catch (Exception e) {
  16. logger.info("unable to set listener priority: {}", e.getMessage());
  17. }
  18. if (!Boolean.getBoolean("p2p.test.inhibitAcceptor")) {
  19. thread.start();
  20. } else {
  21. logger.fatal(
  22. "p2p.test.inhibitAcceptor was found to be set, inhibiting incoming tcp/ip connections");
  23. socket.close();
  24. }
  25. } catch (IOException io) {
  26. String s = "While creating ServerSocket on port " + p;
  27. throw new ConnectionException(s, io);
  28. }
  29. this.port = localPort;
  30. }

代码示例来源:origin: apache/hive

  1. /**
  2. * Finds a free port on the machine.
  3. *
  4. * @return
  5. * @throws IOException
  6. */
  7. public static int findFreePort() throws IOException {
  8. ServerSocket socket= new ServerSocket(0);
  9. int port = socket.getLocalPort();
  10. socket.close();
  11. return port;
  12. }

代码示例来源:origin: stackoverflow.com

  1. ServerSocket s = new ServerSocket(0);
  2. System.out.println("listening on port: " + s.getLocalPort());

代码示例来源:origin: apache/hive

  1. public static int findFreePort() throws IOException {
  2. ServerSocket socket= new ServerSocket(0);
  3. int port = socket.getLocalPort();
  4. socket.close();
  5. return port;
  6. }
  7. }

代码示例来源:origin: OryxProject/oryx

  1. /**
  2. * Binds to a free ephemeral port, and then releases it. The returned port is quite likely
  3. * to be free for use after this, but is not entirely guaranteed to be.
  4. *
  5. * @return a (probably) free ephemeral port
  6. * @throws IOException if an error occurs while binding to a port
  7. */
  8. public static int chooseFreePort() throws IOException {
  9. try (ServerSocket socket = new ServerSocket(0, 0)) {
  10. return socket.getLocalPort();
  11. }
  12. }

代码示例来源:origin: alibaba/mdrill

  1. /**
  2. * ԴãжǷ԰ض˿
  3. *
  4. * @param port
  5. * @return
  6. * @throws IOException
  7. */
  8. public static int try_port(int port) throws IOException {
  9. ServerSocket socket = new ServerSocket(port);
  10. int rtn = socket.getLocalPort();
  11. socket.close();
  12. return rtn;
  13. }

代码示例来源:origin: SonarSource/sonarqube

  1. int getAvailable(InetAddress address) {
  2. try (ServerSocket socket = new ServerSocket(0, 50, address)) {
  3. return socket.getLocalPort();
  4. } catch (IOException e) {
  5. throw new IllegalStateException("Fail to find an available port on " + address, e);
  6. }
  7. }
  8. }

代码示例来源:origin: btraceio/btrace

  1. private static int findFreePort() {
  2. ServerSocket server = null;
  3. int port = 0;
  4. try {
  5. server = new ServerSocket(0);
  6. port = server.getLocalPort();
  7. } catch (IOException e) {
  8. port = DEFAULT_PORT;
  9. } finally {
  10. try {
  11. server.close();
  12. } catch (Exception e) {
  13. // ignore
  14. }
  15. }
  16. return port;
  17. }
  18. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. public int random() throws Exception {
  3. try (ServerSocket serverSocket = new ServerSocket(0)) {
  4. return serverSocket.getLocalPort();
  5. }
  6. }

代码示例来源:origin: jamesdbloom/mockserver

  1. public static int findFreePort() {
  2. int port;
  3. try {
  4. ServerSocket server = new ServerSocket(0);
  5. port = server.getLocalPort();
  6. server.close();
  7. // allow time for the socket to be released
  8. TimeUnit.MILLISECONDS.sleep(250);
  9. } catch (Exception e) {
  10. throw new RuntimeException("Exception while trying to find a free port", e);
  11. }
  12. return port;
  13. }
  14. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. public int specific(int port) throws Exception {
  3. try (ServerSocket serverSocket = new ServerSocket(port)) {
  4. return serverSocket.getLocalPort();
  5. }
  6. }
  7. }

代码示例来源:origin: apache/hive

  1. /**
  2. * Finds a free port on the machine, but allow the
  3. * ability to specify a port number to not use, no matter what.
  4. */
  5. public static int findFreePortExcepting(int portToExclude) throws IOException {
  6. ServerSocket socket1 = null;
  7. ServerSocket socket2 = null;
  8. try {
  9. socket1 = new ServerSocket(0);
  10. socket2 = new ServerSocket(0);
  11. if (socket1.getLocalPort() != portToExclude) {
  12. return socket1.getLocalPort();
  13. }
  14. // If we're here, then socket1.getLocalPort was the port to exclude
  15. // Since both sockets were open together at a point in time, we're
  16. // guaranteed that socket2.getLocalPort() is not the same.
  17. return socket2.getLocalPort();
  18. } finally {
  19. if (socket1 != null){
  20. socket1.close();
  21. }
  22. if (socket2 != null){
  23. socket2.close();
  24. }
  25. }
  26. }

代码示例来源:origin: go-lang-plugin-org/go-lang-idea-plugin

  1. private static int findFreePort() {
  2. try(ServerSocket socket = new ServerSocket(0)) {
  3. socket.setReuseAddress(true);
  4. return socket.getLocalPort();
  5. }
  6. catch (Exception ignore) {
  7. }
  8. throw new IllegalStateException("Could not find a free TCP/IP port to start dlv");
  9. }
  10. }

代码示例来源:origin: org.apache.hadoop/hadoop-common

  1. /**
  2. * Return a free port number. There is no guarantee it will remain free, so
  3. * it should be used immediately.
  4. *
  5. * @returns A free port for binding a local socket
  6. */
  7. public static int getFreeSocketPort() {
  8. int port = 0;
  9. try {
  10. ServerSocket s = new ServerSocket(0);
  11. port = s.getLocalPort();
  12. s.close();
  13. return port;
  14. } catch (IOException e) {
  15. // Could not get a free port. Return default port 0.
  16. }
  17. return port;
  18. }

代码示例来源:origin: apache/flink

  1. /**
  2. * Find a non-occupied port.
  3. *
  4. * @return A non-occupied port.
  5. */
  6. public static int getAvailablePort() {
  7. for (int i = 0; i < 50; i++) {
  8. try (ServerSocket serverSocket = new ServerSocket(0)) {
  9. int port = serverSocket.getLocalPort();
  10. if (port != 0) {
  11. return port;
  12. }
  13. }
  14. catch (IOException ignored) {}
  15. }
  16. throw new RuntimeException("Could not find a free permitted port on the machine.");
  17. }

相关文章