Checks if a port is available by opening a socket on it
@param port the port to test
@return true if the port is available
776769
public static boolean isPortAvailable(int port) {
        StringBuilder builder = new StringBuilder();
        builder.append("Testing port ").append(port).append("\n");
        try (Socket s = new Socket("localhost", port)) {
            // If the code makes it this far without an exception it means
            // something is using the port and has responded.
            builder.append("Port ").append(port).append(" is not available").append("\n");
            return false;
        } catch (IOException e) {
            builder.append("Port ").append(port).append(" is available").append("\n");
            return true;
        } finally {
            log.fine(builder.toString());
        }
    }
498419
public static boolean isValidIpPort(@NotNull final String portStr) {
        return ipPortPattern.matcher(StringUtils.strip(portStr)).matches();
    }
486460
public static boolean isPortAvailable(int port) {
        try {
            ServerSocket socket = new ServerSocket(port);
            socket.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
1309492
public boolean isSocket() throws SftpStatusException, SshException {
		// This is long hand because gcj chokes when it is not? Investigate why
		if ((getAttributes().getPermissions().longValue() & SftpFileAttributes.S_IFSOCK) == SftpFileAttributes.S_IFSOCK) {
			return true;
		}
		return false;
	}
457702
def port(self, port=None):
        
        if (port is None) or (port == self.__port):
            return self.__port
        # when port change ensure old socket is close
        self.close()
        # valid port ?
        if 0 < int(port) < 65536:
            self.__port = int(port)
            return self.__port
        else:
            return None
344504
public boolean isSocket() {
		if (permissions != null
				&& (permissions.longValue() & SftpFileAttributes.S_IFSOCK) == SftpFileAttributes.S_IFSOCK) {
			return true;
		}
		return false;
	}
1241420
public static boolean isPortAvailable(final int port) {

        try (ServerSocket tcp = new ServerSocket(port);
            DatagramSocket udp = new DatagramSocket(port)) {
            return tcp.isBound() && udp.isBound();
        } catch (Exception e) { //NOSONAR
            return false;
        }
    }
658697
def check_port(self, port):
        
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.bind(("localhost", port))
        except socket.error:
            raise UnityWorkerInUseException(self.worker_id)
        finally:
            s.close()
321019
public static boolean available(final String host, final int port)
	{
		Socket socket = null;
		try
		{
			socket = newSocket(host, port);
			return false;
		}
		catch (final IOException ignored)
		{
			return true;
		}
		finally
		{
			if (!closeClientSocket(socket))
			{
				LOGGER.error("Socket could not be closed on host " + host + " on port " + port);
			}
		}
	}
698225
def accepts_port(self, port):
        

        if self.rejected_ports is None and self.accepted_ports is None:
            raise RuntimeError("policy hasn't been set yet")

        if self.rejected_ports:
            for x in self.rejected_ports:
                if port == x:
                    return False
            return True

        for x in self.accepted_ports:
            if port == x:
                return True
        return False
764607
def is_server_running(host, port):
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        return s.connect_ex((host, port)) == 0
    finally:
        s.close()
724807
protected Socket openSocket(String host, int port)
	throws IOException {
        return SocketFactory.getDefault().createSocket(host, port);
    }
994126
def has_port_by_ref(self, port_ref):
        '''

        '''
        with self._mutex:
            if self.get_port_by_ref(self, port_ref):
                return True
            return False
540773
function findAvailablePort(port = 9528) {
    return new Promise((resolve, reject) => {
        const check = () => {
            portIsOccupied(port, (isFound) => {
                if (isFound) {
                    resolve(port);
                } else {
                    port++;

                    if (port > 65535) {
                        reject();
                    } else {
                        check();
                    }
                }
            });
        };

        check();
    });
}
787501
def find_available_local_port():
	
	infos = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM)
	family, proto, _, _, addr = next(iter(infos))
	sock = socket.socket(family, proto)
	sock.bind(addr)
	addr, port = sock.getsockname()[:2]
	sock.close()
	return port
796017
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.");
	}
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;
    }