private final int port;
508751
public static ServiceAgentInfo from(SAAdvert saAdvert)
    {
        IdentifierExtension identifierExtension = IdentifierExtension.findFirst(saAdvert.getExtensions());
        String identifier = identifierExtension == null ? null : identifierExtension.getIdentifier();
        return new ServiceAgentInfo(identifier, saAdvert.getURL(), saAdvert.getScopes(), saAdvert.getAttributes(), saAdvert.getLanguage());
    }
113901
public static int getUsableLocalPort(int minPort, int maxPort) {
		for (int i = minPort; i <= maxPort; i++) {
			if (isUsableLocalPort(RandomUtil.randomInt(minPort, maxPort + 1))) {
				return i;
			}
		}

		throw new UtilException("Could not find an available port in the range [{}, {}] after {} attempts", minPort, maxPort, maxPort - minPort);
	}
347104
private static int getOpenPort() throws IOException {
        Log.info(c, "getOpenPort", "Entry.");
        ServerSocket s = new ServerSocket(0);
        Log.info(c, "getOpenPort", "Got socket.");
        int port = s.getLocalPort();
        Log.info(c, "getOpenPort", "Got port " + s);
        s.close();
        Log.info(c, "getOpenPort", "Close socket");
        return port;
    }
142837
def select_port(default_port=20128):
    """"""
    import socket
    while True:
        try:
            sock = socket.socket(socket.AF_INET,
                                 socket.SOCK_STREAM,
                                 socket.IPPROTO_TCP)
#            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind( ("127.0.0.1", default_port) )
        except socket.error as _msg:  # analysis:ignore
            default_port += 1
        else:
            break
        finally:
            sock.close()
            sock = None
    return default_port
1140433
public static boolean portAvailable(int port) {
        if (port < MIN_PORT || port > MAX_PORT) {
            throw new InvalidArgument("port is out of range");
        }

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
            // Do nothing
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    /* should not be thrown */
                }
            }
        }

        return false;
    }
393260
@Override
  public int portLocal()
  {
    SocketChannel s = _channel;
    
    if (s != null) {
      try {
        InetSocketAddress addr = (InetSocketAddress) s.getLocalAddress();
        
        return addr.getPort();
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
    else {
      return -1;
    }
  }
99552
public int getPort() {
    if (port != null && port != 0) {
      return port;
    }
    return urlParser != null ? urlParser.getHostAddresses().get(0).port : 3306;
  }
298639
@Override
  public synchronized Integer next() {
    if (availablePorts.isEmpty()) {
      return null;
    }
    Collections.sort(availablePorts);
    Integer port = availablePorts.get(0);
    portsInUse.add(port);
    availablePorts.remove(port);
    return port;
  }
171421
@Override
  public int portRemote()
  {
    if (_channel != null) {
      try {
        SocketAddress addr = _channel.getRemoteAddress();
        
        return 0;
      } catch (Exception e) {
        e.printStackTrace();
        return 0;
      }
    }
    else
      return 0;
  }
915565
public static int mapPort(Map<Integer, Integer> mapping, int port) {
    Integer mappedPort = mapping.get(port);
    if( mappedPort == null ) throw new RuntimeException("Could not map port "+port);
    return mappedPort;
  }
896879
public static Endpoint of(String host, int port) {
        validatePort("port", port);
        return create(host, port);
    }
1334777
private void init(int port, File commandFileTmp) throws IOException {
        if (port != -1) {
            serverSocketChannel = SelectorProvider.provider().openServerSocketChannel();

            // Open the socket for loopback only..
            InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(null), port);

            IOException bindError = null;

            boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");

            try {
                //Attempt bind with reuseAddress set to false.
                serverSocketChannel.socket().setReuseAddress(false);
                serverSocketChannel.socket().bind(address);

                //If we are not on Windows and the bind succeeded, we should set reuseAddr=true
                //for future binds.
                if (!isWindows) {
                    serverSocketChannel.socket().setReuseAddress(true);
                    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                        Tr.debug(tc, "ServerSocket reuse set to true to allow for later override");
                    }
                }
            } catch (IOException ioe) {
                // see if we got the error because port is in waiting to be cleaned up.
                // If so, no one should be accepting connections on it, and open should fail.
                // If that's the case, we can set ReuseAddr to expedite the bind process.
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "ServerSocket bind failed on first attempt with IOException: " + ioe.getMessage());
                }
                bindError = ioe;
                try {
                    InetSocketAddress testAddr = new InetSocketAddress(InetAddress.getByName(null), port);
                    if (!testAddr.isUnresolved()) {
                        SocketChannel testChannel = SocketChannel.open(testAddr);
                        // if we get here, socket opened successfully, which means someone is really listening
                        // so close connection and don't bother trying to bind again
                        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                            Tr.debug(tc, "attempt to connect to command port to check listen status worked, someone else is using the port!");
                        }
                        testChannel.close();
                    } else {
                        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                            Tr.debug(tc, "Test connection addr is unresolvable; " + testAddr);
                        }
                    }
                } catch (IOException testioe) {
                    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                        Tr.debug(tc, "attempt to connect to command port to check listen status failed with IOException: " + testioe.getMessage());
                    }
                    try {
                        // open (or close) got IOException, retry with reuseAddress set to true
                        serverSocketChannel.socket().setReuseAddress(true);
                        serverSocketChannel.socket().bind(address);
                        bindError = null;

                    } catch (IOException newioe) {
                        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                            Tr.debug(tc, "ServerSocket bind failed on second attempt with IOException: " + newioe.getMessage());
                        }
                        bindError = newioe;
                    }
                }
            }

            if (bindError == null) {
                // if we requested an ephemeral port, find out what port we ended up with
                if (port == 0) {
                    port = serverSocketChannel.socket().getLocalPort();
                }
                listenForCommands = true;
            } else {
                throw bindError;
            }
        }

        ServerCommandID sci = new ServerCommandID(port, serverUUID);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(commandFileTmp);
            fos.write(sci.getIDString().getBytes());
            fos.close();
        } finally {
            Utils.tryToClose(fos);
        }
    }
486460
public static boolean isPortAvailable(int port) {
        try {
            ServerSocket socket = new ServerSocket(port);
            socket.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
1324664
def client_port(self):
        """"""
        address = self._client.getpeername()
        if isinstance(address, tuple):
            return address[1]

        # Maybe a Unix domain socket connection.
        return 0
592438
protected int getAutoPort(int port) throws BindException {
		if (System.getProperty("os.name").startsWith("Mac")) {
			if (port == 80) {
				port = 8080;
			}
			return port;
		}
		try {
			checkOpened(port);
		}
		catch (BindException e) {
			if (port == 80) {
				checkOpened(8080);
				port = 8080;
			}
			else {
				throw e;
			}
		}
		return port;
	}
226859
private final static int defaultPort(String scheme) {
        if (scheme == null) {
            return -1;
        }
        Integer port = defaultPorts.get(scheme.trim().toLowerCase());
        return (port != null) ? port.intValue() : -1;
    }
1090796
@Override
  public synchronized void release(Integer port) {
    if(port != null){
      portsInUse.remove(port);
      if (port >= minPort && port <= maxPort && isEvenNumber(port)) {
        availablePorts.add(port);
      }
    }
  }