A non-blocking server with java.io

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2012-12-04T22:10:22Z Indexed on 2012/12/04 23:03 UTC
Read the original article Hit count: 166

Filed under:
|
|

Everybody knows that java IO is blocking, and java NIO is non-blocking. In IO you will have to use the thread per client pattern, in NIO you can use one thread for all clients.

Now my question follows: is it possible to make a non-blocking design using only the Java IO api. (not NIO)

I was thinking about a pattern like this (obviously very simplified);

        List<Socket> li;
        for (Socket s : li) {
            InputStream in = s.getInputStream();
            byte[] data = in.available();
            in.read(data);
            // processData(data); (decoding packets, encoding outgoing packets
        }

Also note that the client will always be ready for reading data.

What are your opinions on this? Will this be suitable for a server that should at least hold a few hundred of clients without major performance issues?

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets