Java 8 ServerSocketChannel - 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 ServerSocketChannel class.

This code example shows how to use the following methods:sleep, register, socket, configureBlocking
 
	public static void main(String[] args) {
		try { VocabServer v = new VocabServer(4444);
		Thread t = new Thread(v);
		t.start();
		try {Thread.sleep(10000); } catch (Exception e ) {}
		v.stopServer();
		} catch (IOException e) { e.printStackTrace();}
	} 
	 
	ServerSocketChannel serverChannel;
	public VocabServer(int port) throws IOException {
		serverChannel = ServerSocketChannel.open();
		selector = Selector.open();
		serverChannel.socket().bind (new InetSocketAddress(port));
		serverChannel.configureBlocking (false);
		serverChannel.register (selector, SelectionKey.OP_ACCEPT);		
		System.err.println("Vocab server initialized on port " + port);
	} 
	 

7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:register, bind, isOpen, configureBlocking
		} 
	} 
 
	@Override 
	protected final IoSession bind0(Set<SocketAddress> localAddresses) {
		if (serverSocketChannel.isOpen() && selector.isOpen()) {
			try { 
				serverSocketChannel.bind(localAddresses.iterator().next());
				if (serverSocketChannel.isOpen() && selector.isOpen()) {
					serverSocketChannel.configureBlocking(false);
					serverSocketChannel.register(selector,
							SelectionKey.OP_ACCEPT);
				} 
				// 
				init(); 
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:socket, validOps, isBlocking, isRegistered, provider
            // Android doesn't cache the timeout, so the getsockopt(2) fails and throws. 
        } 
    } 
 
    public void testChannelBasicStatus() { 
        ServerSocket gotSocket = this.serverChannel.socket();
        assertFalse(gotSocket.isClosed());
        assertTrue(this.serverChannel.isBlocking());
        assertFalse(this.serverChannel.isRegistered());
        assertEquals(SelectionKey.OP_ACCEPT, this.serverChannel.validOps());
        assertEquals(SelectorProvider.provider(), this.serverChannel.provider());
    } 
 
    // ------------------------------------------------------------------- 
    // Test for accept() 
    // ------------------------------------------------------------------- 
 
    /* 
     * Test method for 'java.nio.channels.ServerSocketChannel.accept()' 
     */ 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:toString, socket, isOpen, isBlocking
		ch.configureBlocking(false);
	} 
 
	@Override 
	public String toString() {		
		return "InetServerSocket[" + ch.toString()  
			+ " open="+ch.isOpen()
			+ " blocking="+ch.isBlocking()
			+ " addr="+ch.socket().getLocalSocketAddress()
		    + "]";		 
	} 
 
} 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:close
        cnxnFactory.shutdown();
 
        Field ssField = cnxnFactory.getClass().getDeclaredField("ss");
        ssField.setAccessible(true);
        ServerSocketChannel ss = (ServerSocketChannel) ssField.get(cnxnFactory);
        ss.close();
      } 
      close(); 
    } 
    catch (Exception e)
    { 
      e.printStackTrace();
    } 
  } 
 
  public void killPeerZkServer() 
  { 
    ZooKeeperServer zserver = _peer.getActiveServer();
 
    try 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:socket, validOps, isBlocking, isRegistered, provider
            // Android doesn't cache the timeout, so the getsockopt(2) fails and throws. 
        } 
    } 
 
    public void testChannelBasicStatus() { 
        ServerSocket gotSocket = this.serverChannel.socket();
        assertFalse(gotSocket.isClosed());
        assertTrue(this.serverChannel.isBlocking());
        assertFalse(this.serverChannel.isRegistered());
        assertEquals(SelectionKey.OP_ACCEPT, this.serverChannel.validOps());
        assertEquals(SelectorProvider.provider(), this.serverChannel.provider());
    } 
 
    // ------------------------------------------------------------------- 
    // Test for accept() 
    // ------------------------------------------------------------------- 
 
    /* 
     * Test method for 'java.nio.channels.ServerSocketChannel.accept()' 
     */ 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:register, socket, configureBlocking
	public PServer(int port, FileSystem fs, Path ttablePath) throws IOException {
		ttable = new TTable_monolithic_IFAs(fs, ttablePath, true);
		 
		serverChannel = ServerSocketChannel.open();
		selector = Selector.open();
		serverChannel.socket().bind (new InetSocketAddress(port));
		serverChannel.configureBlocking (false);
		serverChannel.register (selector, SelectionKey.OP_ACCEPT);		
		System.err.println("PServer initialized on " + InetAddress.getLocalHost() + ":" + port);
	} 
	 
	public void stopServer() { 
		System.err.println("Stopping PServer...");
		try { selector.close(); serverChannel.close(); } catch (Exception e) { System.err.println("Caught " + e); }
	} 
	 
	Selector selector = null;
	long reqs=0;
	 
	public void run() { 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:accept, socket
This code example shows how to use the following methods:socket, configureBlocking
    ServerSocketChannel ssc;
 
    protected void setUp() throws Exception { 
        super.setUp(); 
        ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        ServerSocket ss = ssc.socket();
        InetSocketAddress address = new InetSocketAddress(PORT);
        ss.bind(address);
        selector = Selector.open();
    } 
 
    protected void tearDown() throws Exception { 
        try { 
            ssc.close();
        } catch (Exception e) {
            // do nothing 
        } 
        try { 
            selector.close();
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:register, socket, configureBlocking
     
    public Listener() throws IOException { 
      address = new InetSocketAddress(bindAddress, port);
      // Create a new server socket and set to non blocking mode 
      acceptChannel = ServerSocketChannel.open();
      acceptChannel.configureBlocking(false);
 
      // Bind the server socket to the local host and port 
      bind(acceptChannel.socket(), address, backlogLength);
      port = acceptChannel.socket().getLocalPort(); //Could be an ephemeral port
      // create a selector; 
      selector= Selector.open();
 
      // Register accepts on the server socket with the selector. 
      acceptChannel.register(selector, SelectionKey.OP_ACCEPT);
      this.setName("IPC Server listener on " + port);
      this.setDaemon(true);
    } 
    /** cleanup connections from connectionList. Choose a random range 
     * to scan and also have a limit on the number of the connections 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
See Code Examples for Java 8 ServerSocketChannel Methods: