2

I'm new with Java NIO, after reading some tutorials, I've tried my own to write a simple NIO server and client. My server just does a simple thing is listen from client and print to console, and the client just connects to server and send to it 3 messages "Hello". The problem is my server listens and works well with the 3 messages, after that it should be blocked and continue listening, but it does not, there's no blocking, it runs it while loop infinitely. Here's my server and client:

Server

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Iterator;
import java.util.Set;

public class Server {
    public static void main(String args[]) throws Exception {
        // Create the server socket channel
        ServerSocketChannel server = ServerSocketChannel.open();
        // nonblocking I/O
        server.configureBlocking(false);
        // host-port 8000
        server.socket().bind(new InetSocketAddress(8000));
        System.out.println("Server actives at port 8000");
        // Create the selector
        Selector selector = Selector.open();
        // Recording server to selector (type OP_ACCEPT)
        server.register(selector, SelectionKey.OP_ACCEPT);

        while (selector.select() > 0) {

            // Get keys
            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> i = keys.iterator();

            // print 
            System.out.println("[ " + keys.size() + " ]");

            // For each keys...
            while (i.hasNext()) {
                SelectionKey key = (SelectionKey) i.next();
                // Remove the current key
                i.remove();

                // if isAccetable = true
                // then a client required a connection
                if (key.isAcceptable()) {
                    // get client socket channel
                    SocketChannel client = server.accept();
                    // Non Blocking I/O
                    client.configureBlocking(false);
                    // recording to the selector (reading)
                    client.register(selector, SelectionKey.OP_READ);
                    continue;
                }

                // if isReadable = true
                // then the server is ready to read
                if (key.isReadable()) {

                    SocketChannel client = (SocketChannel) key.channel();

                    // Read byte coming from the client
                    int BUFFER_SIZE = 1024;
                    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
                    try {
                        client.read(buffer);
                    } catch (Exception e) {
                        // client is no longer active
                        e.printStackTrace();
                    }

                    // Show bytes on the console
                    buffer.flip();
                    Charset charset = Charset.forName("ISO-8859-1");
                    CharsetDecoder decoder = charset.newDecoder();
                    CharBuffer charBuffer = decoder.decode(buffer);
                    System.out.println("[" + charBuffer.toString() + "]");
                }
            }
        }
    }
}

And here's my client:

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class Client {
    public static void main(String args[]) throws Exception {
        // Create client SocketChannel
        SocketChannel client = SocketChannel.open();

        // nonblocking I/O
        client.configureBlocking(false);

        // Connection to host port 8000
        client.connect(new java.net.InetSocketAddress("127.0.0.1", 8000));

        // Create selector
        Selector selector = Selector.open();

        // Record to selector (OP_CONNECT type)
        SelectionKey clientKey = client.register(selector,
                SelectionKey.OP_CONNECT);

        int counter = 0;
        boolean chk = true;

        // Waiting for the connection
        while (selector.select(500) > 0 && chk) {
            // Get keys
            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> i = keys.iterator();

            // For each key...
            while (i.hasNext() && chk) {
                SelectionKey key = (SelectionKey) i.next();

                // Remove the current key
                i.remove();

                // Get the socket channel held by the key
                SocketChannel channel = (SocketChannel) key.channel();

                // Attempt a connection
                if (key.isConnectable()) {

                    // Connection OK
                    System.out.println("Server Found");

                    // Close pendent connections
                    if (channel.isConnectionPending())
                        channel.finishConnect();

                    // Write continuously on the buffer
                    ByteBuffer buffer = null;
                    for (;chk;counter++) {
                        Thread.sleep(1000);
                        buffer = ByteBuffer.wrap(new String(" Client ").getBytes());
                        channel.write(buffer);
                        buffer.clear();

                        if (counter == 2)
                        {
                             chk = false;
                             client.close();
                        }
                    }

                }
            }
        }
    }
}

Anyone can explain what is wrong with my code? Thanks in advance.

Leo
  • 1,433
  • 23
  • 40

3 Answers3

6

You are probably getting an endless stream of EOS-s from the accepted socket channel. You are ignoring the result of read(). You must at least check it for -1 and if so close the channel.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

NIO socket apis are non-blocking. The selector returns the keys that are ready for operation. In case there is nothing ready then it will just keep looping. This is the expected behaviour.

Drona
  • 6,886
  • 1
  • 29
  • 35
  • thanks for your answer, but I have an additional question: why before receiving 3 messages, it's blocked, after that, it's non-blocking? – Leo Mar 16 '12 at 15:43
  • I've review your answer "In case there is nothing ready then it will just keep looping", i think you're right at that point, but the loop should be blocked by `selector.select()`, why it is not? – Leo Mar 17 '12 at 02:40
  • 2
    You cannot detect that the peer disconnected in a non-blocking unless your read fails. So, the select will always return the readable selection key believing that it is ready for read. Now it is your responsiblity to detect if it is valid and cancel the key if the read fails. Change server side as below and you should be fine. SocketChannel client = (SocketChannel) key.channel(); int BUFFER_SIZE = 1024; ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); int bytesread = client.read(buffer); if (bytesread == -1) { key.cancel(); client.close(); continue; } – Drona Mar 17 '12 at 06:11
  • Hi @Drona. I'm experiencing a similar problem. In my code, I did do the channel read check as you suggested but the loop of death didn't go away. And the value returned by select() appears to be always zero in the dead loop after only successfully receiving a single message. Can't figure out why. The github path for my code is https://github.com/mcmay/Utilities/blob/master/Java/MultiEchoServerNIOCmd.java Would you like to take a look at my code and point out any problem in it, which may have caused the problem? Thank you. – Michael May May 22 '20 at 00:23
-2

no blocking because of

server.configureBlocking(false);

in Server.main()

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38