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();
    } 
  } 
   
Full Snippet Info
9
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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;
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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);
  } 
 
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
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
Stop searching for code, let great code find you!  Add Codota to your java IDE
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);
		} 
Full Snippet Info
8
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE

Related Java 8 ServerSocket Questions & Answers:

See Code Examples for Java 8 ServerSocket Methods: