Java 8 ServerSocket - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 ServerSocket class.

This code example shows how to use the following methods:close, getLocalPort, isBound
      int min = socket.getLocalPort();
      int max = min + 100;
      conf.set("TestRange", min+"-"+max);
       
 
      ServerSocket socket2 = new ServerSocket();
      InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
      Server.bind(socket2, address2, 10, conf, "TestRange");
      try { 
        assertTrue(socket2.isBound());
        assertTrue(socket2.getLocalPort() > min);
        assertTrue(socket2.getLocalPort() <= max);
      } finally { 
        socket2.close();
      } 
    } finally { 
      socket.close();
    } 
  } 
   
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:bind, setReuseAddress
            throws TTransportException 
    { 
        try 
        { 
            // Make server socket 
            serverSocket = new ServerSocket();
            // Prevent 2MSL delay problem on server restarts 
            serverSocket.setReuseAddress(true);
            // Bind to listening port 
            serverSocket.bind(bindAddr, listenBacklog);
        } 
        catch (IOException ioe)
        { 
            serverSocket = null;
            throw new TTransportException("Could not create ServerSocket on address " + bindAddr.toString() + ".");
        } 
 
        this.keepAlive = keepAlive;
        this.sendBufferSize = sendBufferSize;
        this.recvBufferSize = recvBufferSize;
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:bind, close, getLocalPort, setReuseAddress
  } 
 
  private static int checkPortIsFree(int port) {
    ServerSocket socket;
    try { 
      socket = new ServerSocket();
      socket.setReuseAddress(true);
      socket.bind(new InetSocketAddress("localhost", port));
      int localPort = socket.getLocalPort();
      socket.close();
      return localPort;
    } catch (IOException e) {
      return -1; 
    } 
  } 
 
  public static boolean pollPort(int port) {
    return pollPort(port, 15, SECONDS);
  } 
 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
    public void createServerSocket() throws IOException { 
 
        ServerSocket socket1 = new ServerSocket();
        ServerSocket socket2 = new ServerSocket();
        ServerSocket socket3 = new ServerSocket();
        ServerSocket socket4 = new ServerSocket();
        InetAddress localhost = InetAddress.getLocalHost();
 
        when(this.factory.createServerSocket("test")).thenReturn(socket1);
        when(this.factory.createServerSocket("test", 1)).thenReturn(socket2);
        when(this.factory.createServerSocket("test", 1, 0)).thenReturn(socket3);
        when(this.factory.createServerSocket("test", 1, 0, localhost)).thenReturn(socket4);
 
        ServerSocket result1 = this.subject.createServerSocket("test");
        ServerSocket result2 = this.subject.createServerSocket("test", 1);
        ServerSocket result3 = this.subject.createServerSocket("test", 1, 0);
        ServerSocket result4 = this.subject.createServerSocket("test", 1, 0, localhost);
 
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket1);
        verify(this.manager.getUnnamedRegistry()).registerSocket(socket2);

8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:getLocalPort, isBound
	} 
 
	/** Initialize a server socket for communicating with the client. */ 
	private void initServerSocket() { 
		try { 
			this.serverSocket = new java.net.ServerSocket(COMM_PORT);
			assert this.serverSocket.isBound();
			if (this.serverSocket.isBound()) {
				System.out.println("SERVER inbound data port " + this.serverSocket.getLocalPort()
						+ " is ready and waiting for client to connect..."); 
			} 
		} catch (SocketException se) {
			System.err.println("Unable to create socket.");
			System.err.println(se.toString());
			System.exit(1);
		} catch (IOException ioe) {
			System.err.println("Unable to read data from an open socket.");
			System.err.println(ioe.toString());
			System.exit(1);
		} 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
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
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:isBound, isClosed
        return null; 
    } 
 
    @Override 
    public boolean isOpen() { 
        return !socket.isClosed();
    } 
 
    @Override 
    public boolean isActive() { 
        return isOpen() && socket.isBound();
    } 
 
    @Override 
    protected SocketAddress localAddress0() {
        return socket.getLocalSocketAddress();
    } 
 
    @Override 
    protected void doBind(SocketAddress localAddress) throws Exception {
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:close, isClosed
			} 
		} 
	} 
 
	public static final void closeSocket(final ServerSocket pServerSocket) {
		if(pServerSocket != null && !pServerSocket.isClosed()) {
			try { 
				pServerSocket.close();
			} catch (final IOException e) {
				Debug.e(e);
			} 
		} 
	} 
 
	// =========================================================== 
	// Inner and Anonymous Classes 
	// =========================================================== 
} 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:close, getLocalPort
  public static int getRandomPort() 
  { 
    ServerSocket server = null;
    try 
    { 
      server = new ServerSocket(0);
      return server.getLocalPort();
    } 
    catch ( IOException e )
    { 
      throw new Error(e);
    } 
    finally 
    { 
      if ( server != null )
      { 
        try 
        { 
          server.close();
        } 

8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:bind, close, setReuseAddress
    } 
 
    private static boolean isTcpPortAvailable(InetSocketAddress localAddress) {
        ServerSocket ss = null;
        try { 
            ss = new ServerSocket();
            ss.setReuseAddress(false);
            ss.bind(localAddress);
            ss.close();
            ss = null;
            return true; 
        } catch (Exception ignore) {
            // Unavailable 
        } finally { 
            if (ss != null) {
                try { 
                    ss.close();
                } catch (IOException ignore) {
                    // Ignore 
                } 

7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Java 8 ServerSocket Questions & Answers:

See Code Examples for Java 8 ServerSocket Methods: