Code example for ServerSocketChannel

Methods: registersocketconfigureBlocking

0
 
    private void listenTCP() throws IOException { 
        if (tcpPort > 0) {
            log.info("Binding TCP to " + tcpPort);
            tcpServer = ServerSocketChannel.open();
            tcpServer.configureBlocking(false);
 
            tcpServer.socket().bind(new InetSocketAddress(tcpPort));
            tcpServer.register(this.acceptSelector, SelectionKey.OP_ACCEPT);
        } 
    } 
 
    private void listenUDP() throws IOException { 
        if (udpPort > 0) {
            log.info("Binding UDP to " + udpPort);
            DatagramChannel udp = DatagramChannel.open();
            udp.socket().bind(new InetSocketAddress(udpPort));
            udp.configureBlocking(false);
            udp.register(acceptSelector, SelectionKey.OP_READ);
            udp.register(sendSelector, SelectionKey.OP_WRITE);