ServerSocket close - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ServerSocket close method.

This code example shows how to use the following methods:close, getLocalPort
 
    protected int findFreePort() throws IOException {
        ServerSocket socket = null;
 
        try { 
            socket = new ServerSocket(0);
 
            return socket.getLocalPort();
        } finally { 
            if (socket != null) {
                socket.close();
            } 
        } 
    } 
 
    @BeforeMethod(alwaysRun = true) 
    public void setUpGlobal() throws Exception {
        port = TestHelper.getEnvVariable("ATMOSPHERE_HTTP_PORT", findFreePort());
        urlTarget = getUrlTarget(port);
        atmoServlet = new AtmosphereServlet();
8
CodeRank
This code example shows how to use the following methods:close
    { 
        if (serverSocket != null)
        { 
            try 
            { 
                serverSocket.close();
            } 
            catch (IOException iox)
            { 
                logger.warn("Could not close server socket.", iox);
            } 
            serverSocket = null;
        } 
    } 
 
    @Override 
    public void interrupt() 
    { 
        // The thread-safeness of this is dubious, but Java documentation suggests 
        // that it is safe to do this from a different thread context 
8
CodeRank
This code example shows how to use the following methods:close
        socket.bind(localAddress, config.getBacklog());
    } 
 
    @Override 
    protected void doClose() throws Exception {
        socket.close();
    } 
 
    @Override 
    protected int doReadMessages(List<Object> buf) throws Exception {
        if (socket.isClosed()) {
            return -1; 
        } 
 
        try { 
            Socket s = socket.accept();
            try { 
                buf.add(new OioSocketChannel(this, s));
                return 1; 
            } catch (Throwable t) {
7
CodeRank
This code example shows how to use the following methods:close
        } 
    } 
 
    public static void safeClose(final ServerSocket serverSocket) {
        if (serverSocket != null) try {
            serverSocket.close();
        } catch (IOException e) {
            ROOT_LOGGER.failedToCloseServerSocket(e, serverSocket);
        } 
    } 
 
    public static void safeFinish(final Marshaller marshaller) {
        if (marshaller != null) try {
            marshaller.finish();
        } catch (IOException e) {
            ROOT_LOGGER.failedToFinishMarshaller(e, marshaller);
        } 
    } 
 
    public static void safeFinish(final Unmarshaller unmarshaller) {
7
CodeRank
This code example shows how to use the following methods:close, setReuseAddress
     * @param port the port to check for availability 
     * @return <tt>true</tt> if the port is available, <tt>false</tt> otherwise 
     */ 
    private boolean available(int port) {
        try { 
            ServerSocket ss = new ServerSocket(port);
            try { 
                ss.setReuseAddress(true);
            } finally { 
                ss.close();
            } 
            DatagramSocket ds = new DatagramSocket(port);
            try { 
                ds.setReuseAddress(true);
            } finally { 
                ds.close();
            } 
            return true; 
        } catch (IOException e) {
            return false; 
7
CodeRank
This code example shows how to use the following methods:close
  } 
   
  @Override 
  public void close() throws IOException { 
    try { 
      serverSocket.close();
    } catch(IOException e) {
      LOG.error("error closing TcpPeerServer: ", e);
    } 
  } 
 
  @Override 
  public String toString() {
    return "TcpPeerServer(" + getListeningString() + ")"; 
  } 
} 
7
CodeRank
This code example shows how to use the following methods:close, getLocalPort
        try { 
            try { 
                serverSocket = new ServerSocket(DEFAULT_PORT);
            } catch (IOException e) {
                try { 
                    serverSocket = new ServerSocket(0);
                } catch(IOException e2) {
                    return String.valueOf(DEFAULT_PORT);
                } 
            } 
            return String.valueOf(serverSocket.getLocalPort());
        } finally { 
            if(serverSocket != null) {
                try { 
                    serverSocket.close();
                } catch (IOException e) {}
            } 
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:accept, bind, close, setReuseAddress, setSoTimeout
     * @throws IOException 
     */ 
    public Socket establishSourceSideConnection(final String uniqueUserName, final Socket controlConnection)
            throws IOException {
        final ServerSocket serverSocket = this.createBoundServerSocket(uniqueUserName, controlConnection);
        logger.info("listen on serverSocket {}", serverSocket); //$NON-NLS-1$
        this.sendConnectionRequest(uniqueUserName, controlConnection);
        final Socket socket = serverSocket.accept();
        serverSocket.close();
        return socket;
    } 
 
    private ServerSocket createBoundServerSocket(final String uniqueUserName, final Socket controlConnection)
            throws IOException {
        final ServerSocket serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(controlConnection.getLocalAddress(), controlConnection.getLocalPort()));
        serverSocket.setSoTimeout(this.socketTimeoutInMillis);
        return serverSocket;
    } 

7
CodeRank
This code example shows how to use the following methods:close, getLocalPort
      } 
    } 
  } 
 
  private int findFreePort() throws IOException {
    ServerSocket socket= new ServerSocket(0);
    int port = socket.getLocalPort();
    socket.close();
    return port;
  } 
} 
7
CodeRank
This code example shows how to use the following methods:close, getLocalPort
     * @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;
    } 
 
    public static int available_port() { 
        return available_port(0); 
    } 
 
    /** 
     * ²âÊÔËùÓ㬳¢ÊÔ°ó¶¨prfered¶Ë¿Ú£¬Èç¹ûÒѾ­±»Õ¼Óã¬ÔòÓÉϵͳËæ»ú·ÖÅä 
     *  
     * @param prefered 
     * @return 
7
CodeRank

Related ServerSocket close Questions & Answers:

close Method Overview
Closes this socket.
See Code Examples for other Java 8 ServerSocket Methods: