Non-Blocking Server closes his socketChannels on his own (10 dukes)

Hi,
I have created a server using the new nio package. Clients can connect and disconnect as they wish : the client has a GUI with a button "connect" and a button "disconnect". When the client engages a new connection with the server, the ServerSocketChannel returns a new SocketChannel. I keep the socket connection alive until the client clicks on the "disconnect" button : it sends -1 to the server (socket.close() on the client), the SocketChannel reads it, and I close the SocketChannel.
This works perfect with multiple clients, until... I wait 30mn...
Exemple :
1) I connect 1 PC to the server.
2) The socket connection with this client is alive. On the server, I will name the SocketChannel that manages this connection "socketChannel1".
3) I wait 30mn, and I connect an other PC to the server.
4) Then, just at this very moment, "socketChannel1" receives -1 ==> my connection is closed as I haven't asked for anything.
NB : If I don't wait the 30mn (approximatively), everything is ok. The "socketChannel1" doesn't disconnect ever.
NB2 : If I connect my 2 clients to the server, I wait 30mn, and then I disconnect one of the clients, then the other automatically disconnects (socketChannel receives -1).
NB3 : If there is 2, 3 or more clients... I wait 30mn, and one of the clients disconnect ==> everybody disconnects.
I wonder if there is a timeout or something for the SocketChannel class, or the Select class... Seems strange... Possible?
I would really appreciate any help. I'm on this problem for days, and I still don't see. If ever you had any idea...
Thank you very much.
If you want more precisions, code, or something... You can ask. Thank you.

I have perhaps an idea of what is going on...
On my server, when a client connects, he writes on the socketChannels of the other connected clients (to notify them the connection). I don't use the "key.isWritable()" state that is in the while of the "selector.select()". Perhaps is that an error. I write on all the socketChannels in the "if (key.isReadable())" :
if (key.isReadable()) {
     for (int i=0; i<openedSocketChannelNumber; i++) {
          SocketChannel channel = (SocketChannel) vector.get(i);
          channel.write("connect"); // I made it short here...
perhaps it isn't supported... I'll try to use the "if (key.isWritable) { }" to write on the other socketChannels.
If any idea... Still and always welcome.

Similar Messages

  • NIO Non-Blocking Server not Reading from Key

    I have created a NIO non blocking server (below) and it will not pick up any input from the client.... My log doesnt even show that it enters the readKey() method, so it must be something before. Any help would be appreciated.
    Scott
    package jamb.server;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.channels.ClosedChannelException;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.nio.channels.spi.SelectorProvider;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.logging.Logger;
    import java.util.prefs.Preferences;
    import jamb.server.client.Client;
    public class Server {
            private Selector selector;
            private ServerSocketChannel serverChannel;
            private static Logger logger = Logger.getLogger("jamb.server");
            private static Preferences prefs =  Preferences.systemRoot().node("/jamb/server");
            public void init() {
                    logger.entering("jamb.server.Server", "init");
                    //Get a selector...
                    try {
                            selector = SelectorProvider.provider().openSelector();
                            //Open the SocketChannel and make it non-blocking...
                            serverChannel = ServerSocketChannel.open();
                         serverChannel.configureBlocking(false);
                            //Bind the server to the port....
                            int port = prefs.getInt("Port", 4000);
                            logger.config("Server configured on port " + port + " (default: 4000)");
                         InetSocketAddress isa = new InetSocketAddress(
                                    InetAddress.getLocalHost(), port);       
                         serverChannel.socket().bind(isa);
                    } catch (IOException ioe) {
                            logger.severe ("IOException during server initialization!");
                    logger.exiting("jamb.server.Server", "init");
            public void run() {
                    logger.entering("jamb.server.Server", "run");
                    int bufferSize = prefs.getInt("BufferSize", 8);
                    logger.config("Buffer size set to " + bufferSize + " (default: 8)");
                    SelectionKey acceptKey = null;
                    try {
                            acceptKey = serverChannel.register(
                                    selector, SelectionKey.OP_ACCEPT);
                    } catch (ClosedChannelException cce) {
                    try {
                            while (acceptKey.selector().select() > 0) {
                                    Set readyKeys = selector.selectedKeys();
                                    Iterator i = readyKeys.iterator();
                                    while (i.hasNext()) {
                                            //logger.finest("Processing keys...");
                                            //Get the key from the set and remove it
                                            SelectionKey currentKey = (SelectionKey) i.next();
                                            i.remove();
                                            if (currentKey.isAcceptable()) {
                                                    logger.finest("Accepting key...");
                                                    acceptKey(currentKey);
                                            } else if (currentKey.isReadable()) {
                                                    logger.finest("Reading key...");
                                                    readKey(currentKey, bufferSize);
                                            } else if (currentKey.isWritable()) {
                                                    //logger.finest("Writing key...");
                                                    writeKey(currentKey);
                    } catch (IOException ioe) {
                            logger.warning("IOException during key handling!");
                    logger.exiting("jamb.server.Server", "run");
            public void flushClient (Client client) {
                    try {
                            ByteBuffer buf = ByteBuffer.wrap( client.getOutputBuffer().toString().getBytes());
                            client.getChannel().write(buf);
                    } catch (IOException ioe) {
                            System.out.println ("Error writing to player");
                    client.setOutputBuffer(new StringBuffer());
            private void acceptKey (SelectionKey acceptKey) {
                    logger.entering("jamb.server.Server", "acceptKey");
                    //Retrieve a SocketChannel for the new client, and register a new selector with
                    //read/write interests, and then register
                    try {
                            SocketChannel channel =  ((ServerSocketChannel) acceptKey.channel()).accept();
                            channel.configureBlocking(false);
                            SelectionKey readKey = channel.register(
                                    selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE  );
                            readKey.attach(new Client(this, channel));
                    } catch (IOException ioe) {
                            System.out.println ("Error accepting key");
                    logger.exiting("jamb.server.Server", "acceptKey");
            private void readKey (SelectionKey readKey, int bufSize) {
                    logger.entering("jamb.server.Server", "readKey");
                    Client client = (Client) readKey.attachment();
                    try {
                            ByteBuffer byteBuffer = ByteBuffer.allocate(bufSize);
                            int nbytes = client.getChannel().read( byteBuffer );
                            byteBuffer.flip();
                            Charset charset = Charset.forName( "us-ascii" );
                            CharsetDecoder decoder = charset.newDecoder();
                            CharBuffer charBuffer = decoder.decode(byteBuffer);
                            String text = charBuffer.toString();
                            client.getInputBuffer().append(text);
                            if ( text.indexOf( "\n" ) >= 0 )
                                    client.input();
                    } catch (IOException ioe) {
                            logger.warning("Unexpected quit...");
                            client.disconnect();
                    logger.exiting("jamb.server.Server", "readKey");
            private void writeKey (SelectionKey writeKey) {
                    //logger.entering("jamb.server.Server", "writeKey");
                    Client client = (Client) writeKey.attachment();
                    if (!client.isConnected()) {
                            client.connect();
                    //logger.exiting("jamb.server.Server", "writeKey");

    From my own expierence with the NIO (Under Windows XP/ jdk1.4.1_01); you can't seem to set READ and WRITE at the same time.
    The program flow I usually end up with for a echo server is:
    When the selector.isAcceptable(): accept a connection; register for READs
    In the read event; write the incoming characters to a buffer; register for a WRITE and add the buffer as an attachment.
    In the write event; write the data to the socket If all the data was written; register for a READ; otherwise register for another WRITE so that you can write the rest.
    Not sure if that the "proper" way; but it works well for me.
    - Chris

  • Non-blocking connection on a SocketChannel?

    I'm trying out the non-blocking connection of a SocketChannel. So I wrote the following test code and supply a list of IPs (both good and bad IPs). But disregard the IPs I always get the result of a channel being in connection pending state (even for some bogus IPs). Any help will be great.
    public class ConnectTest
        public static void main(String[] args) throws
    Exception
            List<SocketChannel> channels = new ArrayList<SocketChannel>();
            for ( String s: args )
                SocketChannel channel = SocketChannel.open();
                channel.configureBlocking(false);
                channels.add(channel);        
                InetSocketAddress remote = new InetSocketAddress(s, 80);
                channel.connect(remote);
            System.out.println("wait for timeout...");
            Thread.sleep(10000);
            for ( SocketChannel c : channels )
                if ( c.isConnected() )
                    System.out.println(c.socket().getInetAddress() + " connected ");
                else if ( c.isConnectionPending() )
                    System.out.println(c.socket().getInetAddress() + " connection pending ");               
                else
                    System.out.println(c.socket().getInetAddress() + " timeout ");
                c.close();
    }

    Forget the sleep: use a Selector, selecting on OP_CONNECT. When you get it, call SocketChannel.finishConnect() for the channel(s) selected. If that method returns 'true', the connection is complete. If it returns 'false', the connection is still pending (and in fact OP_CONNECT should not have fired); if it throws an exception, the connection attempt has failed.
    If the connection is complete you must also deregister it for OP_CONNECT before registering it for OP_READ or OP_WRITE.

  • Non-blocking Vectored read from SocketChannel fails

    Hi.
    Please look at the code example:
    ByteBuffer tmp1 = ByteBuffer.allocate(500);
    ByteBuffer tmp2 = ByteBuffer.allocate(500);
    ByteBuffer[] tmp = {tmp1, tmp2};
    while ((count = socketChannel.read(tmp)) > 0) {
    + "bytes.");
    When I run this code, (using non-blocking socket channel), the first buffer is read ok, but on the second round of the while loop, I get an IOException, saying: "a non blocking socket operation could not be completed immediately".
    If I put 'tmp1' instead of 'tmp' in the read(), (i.e - use a normal buffer), the read successfully finishes.
    Why?
    I found nothing in the documentation, and in the 'Java NIO' book (Ron Hitchens).
    Help?
    Thanks.

    Please ignore the + "bytes."); inside the while.

  • The non master server is shutting down on its own?

    Hey all,
    I just started have this problem this week. With HA configured I am having the non-master host restarting on it's own. I have been unable to find anything in the logs that would explain this.
    Shared storage is on a FC SAN device.
    2 Dell 2950 III servers
    Oracle VM 2.1.2
    Any Ideas? or is anyone else seeing this?

    Check for ocfs2 errors on the master -- the new versions of ocfs2 automatically reboot a fenced node, which is what sounds like is happening to your non-master server. You may have to run the /etc/init.d/o2cb configure on both servers to increase the polling/heartbeat time between the two boxes, so that ocfs2 doesn't fence itself.

  • Non-blocking Server/Client is blocking...?!

    First, a simple question:
    Why in most example code using Selectors, when iterating over the Set returned by Selector.selectedKeys() does the currently selected SelectionKey get removed from the Iterator? I see it all the time, but since the SelectionKey is still bound to the underlying Selector it seems to not really do anything.
    Now to my main problem:
    I been working with 1.4 for a week now trying to implement a simple test server/client, whereby the server constantly sends out the current time to any subscribing clients, who in turn display the time to stdout. Pretty straight forward, huh?
    Well, everything seems to go fine...once. The Server accepts the client connection, sends out a the current time, the client (only one for now) receives it, displays to the screen, but then both server and client block on the Selector.select() method.
    If I shut down the client, the server then continues through the select() method, finding one SelectionKey, tries to write to it and throws an IOException (since the client is no more). I'm catching that exception and then removing the channel from the Selector, so that the server may continue to service requests.
    When starting a second client while the first is still running causes the following sequence of events:
    Server: starts up
    ClientA: connects to server
    Server: broadcasts the time
    ClientA: displays time
    // nothing else happens until...
    ClientB: connects to server
    Server: broadcasts the time
    ClientA: displays time
    ClientB: displays time
    //everything blocks again...
    ClientA: disconnects from server
    Server: broadcasts the time
    ClientB: displays time
    As you can see it seems everything blocks until a client does something (connect/disconnect).
    I will post code if anyone asks, but I don't want to spam the board if no one is willing to help me.
    -Justin

    You got it!
    //   ********** SERVER **********
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.nio.ByteBuffer;
    import java.nio.channels.CancelledKeyException;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.Set;
    public class TestServer
      private static final int DEFAULT_PORT = 9999;
      public static void main(String[] args)
        TestServer s = new TestServer();
      }//end of main(String[])
      public TestServer()
        this(DEFAULT_PORT);
      }// end of TestServer()
      public TestServer(int port)
        InetSocketAddress addr = new InetSocketAddress(port);
        try
          Selector acceptSelector = Selector.open();
          Selector broadcastSelector = Selector.open();
          ConnectionList connections = new ConnectionList(broadcastSelector);
          Acceptor a = new Acceptor(acceptSelector, addr, connections);
          Broadcaster b = new Broadcaster(broadcastSelector, connections);
          a.start();
          b.start();
        }// end of try
        catch (Exception ex)
          ex.printStackTrace();
        }// end of catch
      }// end of TestServer(int)
      private static String status(Selector s)
        StringBuffer sb = new StringBuffer(100);
        sb.append("Selector: ");
        Set keys = s.keys();
        sb.append("\n\tNum Keys: ");
        sb.append(keys.size());
        Iterator iter = keys.iterator();
        int i = 0;
        while (iter.hasNext())
          try
            sb.append("\n\t[");
            sb.append(i++);
            sb.append("]:");
            SelectionKey key = (SelectionKey)iter.next();
            sb.append(" acceptable=");
            sb.append(key.isAcceptable());
            sb.append(" connectable=");
            sb.append(key.isConnectable());
            sb.append(" readable=");
            sb.append(key.isReadable());
            sb.append(" writable=");
            sb.append(key.isWritable());
          }// end of try
          catch (CancelledKeyException cke)
            sb.append("*** CANCELLED KEY");
          }// end of catch
        }// end of while
        return sb.toString();
      }// end of status(Selector)
      class Broadcaster extends Thread
        private final String TF = Broadcaster.class.getName();
        private int BUFFER_SIZE = 2048;
        private Selector selector_;
        private ConnectionList connections_;
        private ByteBuffer buffer_;
        public Broadcaster(Selector selector, ConnectionList connections)
          super("Broadcaster");
          selector_ = selector;
          connections_ = connections;
          buffer_ = ByteBuffer.allocateDirect(BUFFER_SIZE);
        public void run()
          while (true)
            try
              registerNewChannels();
              System.out.println("BroadcasterThread: Before select() "+status(selector_));
              System.out.println("BroadcasterThread: Selecting...");
              int keysReady = selector_.select();
              System.out.println("BroadcasterThread: After select() "+status(selector_));
              System.out.println("BroadcasterThread: "+keysReady+" ready Key(s)");
              if (keysReady > 0)
                transmit();
              }// end of if
            }// end of try
            catch (Exception ex)
              ex.printStackTrace();
              return;
            }// end of catch
          }// end of while
        protected void registerNewChannels()
        throws Exception
          SocketChannel channel = null;
          while (null != (channel = connections_.removeFirst()))
            channel.configureBlocking(false);
            channel.register(selector_, SelectionKey.OP_WRITE);
            System.out.println("BroadcasterThread: Registered connection from " + channel.socket().getInetAddress());
          }// end of while 
        }// end of registerNewChannels()
        public void transmit()
        throws Exception
          Set readyKeys = selector_.selectedKeys();
          System.out.println("BroadcasterThread: Selected Keys: "+readyKeys.size());
          SelectionKey tempKey = null;
          SocketChannel tempChannel = null;
          fillBuffer();
          for (Iterator i = readyKeys.iterator(); i.hasNext(); )
            tempKey = (SelectionKey)i.next();
            tempChannel = (SocketChannel)tempKey.channel();
            if (tempKey.isWritable())
              System.out.println("BroadcasterThread: Key selected is Writable");
              try
                tempChannel.write(buffer_);
                System.out.println("BroadcasterThread: Sent message to "+tempChannel.socket().getInetAddress());
              }// end of try
              catch (IOException ioe)
                System.err.println("BroadcasterThread: Lost Connection");
                tempChannel.close();
              }// end of catch
            }// end of if
            else
              System.out.println("BroadcasterThread: Key selected is not Writable");
            }// end of else
          }// end of for
          buffer_.clear();
        }// end of transmit()
        private void fillBuffer()
          buffer_.clear();
          /* Place Date in Buffer */
          long time = System.currentTimeMillis();
          Date d = new Date(time);
          System.out.println("BroadcasterThread: Broadcasting "+d);
          String date = d.toString()+"\n";
          byte[] datebuff = date.getBytes();
          buffer_.put(datebuff);
          /* Prepare for read operations */
          buffer_.flip();
        }// end of fillBuffer()
      }//end of inner class Broadcaster
      class Acceptor extends Thread
        private Selector selector_;
        private ConnectionList connList_;
        private final String TF = Acceptor.class.getName();
        public Acceptor(Selector selector, InetSocketAddress address, ConnectionList connList)
          super("Acceptor");
          selector_ = selector;
          connList_ = connList;
          try
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.socket().bind(address);
            System.out.println("AcceptorThread: Bound to " + address);
            ssc.register(selector_, SelectionKey.OP_ACCEPT);
          }// end of try
          catch (Exception ex)
            ex.printStackTrace();
          }// end of catch
        public void run()
          while (true)
            try
              System.out.println("AcceptorThread: Selecting...");
              int keysReady = selector_.select();// block till a channel is ready
              System.out.println("AcceptorThread: "+keysReady+" Keys Ready");
              if (keysReady > 0)
                acceptPendingConnections();
              }// end of if
            catch (Exception ex)
              ex.printStackTrace();
        protected void acceptPendingConnections()
        throws Exception
          Set readyKeys = selector_.selectedKeys();
          System.out.println("AcceptorThread: Selected "+readyKeys.size()+" Keys");
          for (Iterator i = readyKeys.iterator(); i.hasNext(); )
            SelectionKey key = (SelectionKey)i.next();
            i.remove();  
            ServerSocketChannel readyChannel = (ServerSocketChannel)key.channel();
            SocketChannel incomingChannel = readyChannel.accept();
            System.out.println("AcceptorThread: Connection from " + incomingChannel.socket().getInetAddress());
            connList_.add(incomingChannel);
          }// end of for
        }// end of acceptPendingConnections()
      }// end of inner class Acceptor
      class ConnectionList
        private LinkedList list_;
        private Selector selectorToNotify_;
        public ConnectionList(Selector toNotify)
          list_ = new LinkedList();
          selectorToNotify_ = toNotify;
        }// end of ConnectionList(Selector)
        public synchronized void add(SocketChannel newlyConnectedChannel)
          list_.add(newlyConnectedChannel);
          selectorToNotify_.wakeup();
        }// end of add(SocketChannel)
        public synchronized SocketChannel removeFirst()
          SocketChannel first = null;
          if (list_.size() > 0)
            first = (SocketChannel)list_.removeFirst();
          }//end of if
          return first;
        }// end of removeFirst()
      }// end of inner class ConnectionList
    }// end of class TestServer
    //   ********** CLIENT **********
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.CancelledKeyException;
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.SocketChannel;
    import java.nio.channels.WritableByteChannel;
    import java.util.Iterator;
    import java.util.Set;
    public class TestClient
      public static final String TF = TestClient.class.getName();
      private static final String DEFAULT_SERVER_IP = "127.0.0.1";
      private static final int DEFAULT_SERVER_PORT = 9999;
      private InetSocketAddress serverAddr_; 
      private Selector connectSelector_;
      private Selector readSelector_; 
      private WritableByteChannel outputChannel_; 
      private ByteBuffer receiveBuffer_;
      private SocketChannel serverChannel_;
      public static void main(String[] args)
        TestClient c = new TestClient();
      }// end of main(String[])
      public TestClient()
        this(DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT);
      }// end of TestClient()
      public TestClient(String ip, int port)
        try
          serverAddr_ = new InetSocketAddress(ip, port);
          connectSelector_ = Selector.open();
          readSelector_ = Selector.open();
          outputChannel_ = Channels.newChannel(System.out);
          receiveBuffer_ = ByteBuffer.allocateDirect(512);
          connect();
          run();
        }// end of try
        catch (Exception ex)
          ex.printStackTrace();
        }// end of catch
      }// end of TestClient(String, int)
      private void connect()
      throws Exception
        serverChannel_ = SocketChannel.open();
        serverChannel_.configureBlocking(false);
        serverChannel_.connect(serverAddr_);
        serverChannel_.register(connectSelector_, SelectionKey.OP_CONNECT);
        int numKeys = 0;
        while (numKeys <= 0)
          System.out.println("connect(): Selecting...");
          numKeys = connectSelector_.select();
          System.out.println("connect(): "+numKeys+" ready Key(s)");
          Set readyKeys = connectSelector_.selectedKeys();
          System.out.println("connect(): Selected Keys: "+readyKeys.size());
          if (numKeys > 0)
            SelectionKey tempKey = null;
            SocketChannel tempChannel = null;
            Iterator i = readyKeys.iterator();
            while (i.hasNext())
              tempKey = (SelectionKey)i.next(); 
              i.remove();  
              tempChannel = (SocketChannel)tempKey.channel();
              if (tempKey.isConnectable())
                System.out.println("connect(): Key selected is Connectable");
                if (tempChannel.isConnectionPending())
                  System.out.println("connect(): Connection Pending");
                  tempChannel.finishConnect();
                  System.out.println("connect(): Connection Completed");
                }// end of if
              }// end of if
              else
                System.out.println("connect(): Key selected is not Connectable");
              }// end of else
            }// end of while
          }// end of if
        }// end of while
      }// end of connect()
      private void run()
      throws Exception
        serverChannel_.register(readSelector_, SelectionKey.OP_READ);
        while (true)
          System.out.println("run(): Before select() "+status(readSelector_));
          System.out.println("run(): Selecting...");
          int numKeys = readSelector_.select();
          System.out.println("run(): After select() "+status(readSelector_));
          System.out.println("run(): "+numKeys+" Ready Key(s)");
          if (numKeys > 0)
            processKeys();
          }// end of if
        }// end of while
      }// end of run()
      private void processKeys()
      throws Exception
        Set readyKeys = readSelector_.selectedKeys();
        System.out.println("processKeys(): Selected Keys: "+readyKeys.size());
        SelectionKey tempKey = null;
        SocketChannel tempChannel = null;
        for (Iterator i = readyKeys.iterator(); i.hasNext(); )
          tempKey = (SelectionKey)i.next();
          tempChannel = (SocketChannel)tempKey.channel();
          if (tempKey.isReadable())
            System.out.println("processKeys(): Key selected is Readable");
            try
              tempChannel.read(receiveBuffer_);
              receiveBuffer_.flip();
              outputChannel_.write(receiveBuffer_);
              receiveBuffer_.clear();
            }// end of try
            catch (IOException ioe)
              System.out.println("processKeys(): Lost Connection");
              tempKey.cancel();
            }// end of catch
          }// end of if
          else
            System.out.println("processKeys(): Key selected is not Readable");
          }// end of else
        }// end of for
      }// end of processKeys()
      private static String status(Selector s)
        StringBuffer sb = new StringBuffer(100);
        sb.append("Selector: ");
        Set keys = s.keys();
        sb.append("\n\tNum Keys: ");
        sb.append(keys.size());
        Iterator iter = keys.iterator();
        int i = 0;
        while (iter.hasNext())
          try
            sb.append("\n\t[");
            sb.append(i++);
            sb.append("]:");
            SelectionKey key = (SelectionKey)iter.next();
            sb.append(" acceptable=");
            sb.append(key.isAcceptable());
            sb.append(" connectable=");
            sb.append(key.isConnectable());
            sb.append(" readable=");
            sb.append(key.isReadable());
            sb.append(" writable=");
            sb.append(key.isWritable());
          }// end of try
          catch (CancelledKeyException cke)
            sb.append("*** CANCELLED KEY");
          }// end of catch
        }// end of while
        return sb.toString();
      }// end of status(Selector)
    }// end of class TestClient

  • Non-blocking server

    Hi there,
    I've got a problem with the NIO package: I'm trying to make a simple webserver, just to get familiar with SocketChannel, ServerSocketChannel, Selector, Buffer's and so on. My problem is that my server can only receive one single request, then it ignores the rest. This is the code:
        private void setupServer() throws IOException
            selector = Selector.open();
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.socket().bind(new InetSocketAddress(80));
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
        public synchronized void start() {
            setPriority(Thread.MIN_PRIORITY); //If I don't set this to min, it consumes a LOT of resources...
            try {
                setupServer();
            } catch (IOException e) {
                throw new RuntimeException("Failed to start server.",e);
            super.start();
        public void run() {
            while(true)
                try {
                    while(selector.select() > 0)
                        //This code will run ONCE
                        Iterator i = selector.keys().iterator();
                        while(i.hasNext())
                            SelectionKey key = (SelectionKey)i.next();
                            i.remove();
                            if(key.isAcceptable())
                                ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
                                SocketChannel channel = serverChannel.accept();
                                //Register the channel somewhere, read from it and close it
                } catch (IOException e) {
                    throw new RuntimeException("Server Crashed!",e);
        }What have I done wrong? It is something wrong, since it only receive one request... Please help me...
    Nille

    You might try changing the while condition to selector.select()>=0.
    If you call wakeup or something in other parts of the code it will cause the select call to return 0 which in your case will cause the server to stop.

  • Are the experts wrong about non-blocking SocketChannels?

    Everyone says to use the new SocketChannel and Selector for "highly scalable" server applications. So I ran a couple of tests using non-blocking (via Selector) and thread-per-connection SocketChannels.
    The Selector version consumes 8x more cpu than the thread-per-connecton version!
    Using JDK 1.4.1 FCS on Win2K, with 1000 socket connections each sending 1k bytes per second, the Selector version was consuming 40% cpu with 10 threads; the thread-per-connection version (using blocking SocketChannels) was only consuming about 5% cpu with 1009 threads.
    So, are the experts wrong? Is there a performance problem when number of SocketChannels exceed a certain threshold?
    For anyone interested, here's the source code:
    Non-Blocking Server using Selector
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.nio.charset.CharsetEncoder;
    import java.nio.channels.FileChannel;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class Server4 implements Runnable
    private static int port = 80;
    public static void main(String args[]) throws Exception
    Server4 server = new Server4();
    Thread thread = new Thread(server);
    thread.setDaemon(true);
    thread.start();
    thread.join();
    public Server4() throws IOException
    ServerSocketChannel server = ServerSocketChannel.open();
    InetSocketAddress isa = new InetSocketAddress(port);
    server.configureBlocking(false);
    server.socket().bind(isa);
    m_selector = Selector.open();
    server.register(m_selector, SelectionKey.OP_ACCEPT);
    Charset utf8 = Charset.forName("UTF-8");
    m_decoder = utf8.newDecoder();
    m_encoder = utf8.newEncoder();
    public void run()
    int count = 0;
    try
    ByteBuffer buffer = ByteBuffer.allocateDirect(2048);
    //FileOutputStream fos = new FileOutputStream("server4.dat");
    //FileChannel fc = fos.getChannel();
    while (m_selector.select() > 0)
    Set keys = m_selector.selectedKeys();
    for (Iterator itr = keys.iterator(); itr.hasNext(); )
    SelectionKey key = (SelectionKey) itr.next();
    itr.remove();
    if (key.isAcceptable())
    System.out.println("accept: " + (++count));
    ServerSocketChannel server
    = (ServerSocketChannel) key.channel();
    SocketChannel channel = server.accept();
    channel.configureBlocking(false);
    channel.register(m_selector, SelectionKey.OP_READ);
    else
    SocketChannel channel = null;
    try
    if (key.isReadable())
    channel = (SocketChannel) key.channel();
    int bytes = channel.read(buffer);
    if (bytes <= 0) // Linux does not throw IOException
    channel.close(); // will also cancel key
    System.out.println("connection closed " + count);
    else
    buffer.flip();
    //fc.write(buffer);
    buffer.clear();
    catch (IOException ioe) // connection closed by client
    System.out.println("readable: " + ioe.getMessage());
    sm_logger.log(Level.INFO, ioe.getMessage(), ioe);
    Throwable cause = ioe.getCause();
    if (cause != null)
    System.out.println("cause: "
    + cause.getClass().getName()
    + ": " + cause.getMessage());
    channel.close(); // will also cancel key
    --count;
    catch (IOException e)
    System.out.println("run: " + e.getMessage());
    sm_logger.log(Level.SEVERE, e.getMessage(), e);
    catch (Exception e)
    System.out.println("run: " + e.getMessage());
    sm_logger.log(Level.SEVERE, e.getMessage(), e);
    private Selector m_selector;
    private CharsetDecoder m_decoder;
    private CharsetEncoder m_encoder;
    private static Logger sm_logger = Logger.getLogger("Server");
    Thread-Per-Connection Server
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.nio.charset.CharsetEncoder;
    import java.nio.channels.FileChannel;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class MultiThreadServer implements Runnable
    private static int port = 80;
    public static void main(String[] args) throws Exception
    ServerSocketChannel server = ServerSocketChannel.open();
    InetSocketAddress isa = new InetSocketAddress(port);
    server.socket().bind(isa);
    int count = 0;
    while (true)
    SocketChannel channel = server.accept();
    System.out.println("accept: " + (++count));
    MultiThreadServer worker = new MultiThreadServer(channel);
    Thread thread = new Thread(worker);
    thread.setDaemon(true);
    thread.start();
    public MultiThreadServer(SocketChannel channel) throws IOException
    m_channel = channel;
    public void run()
    ByteBuffer buffer = ByteBuffer.allocateDirect(2048);
    int bytes = 0;
    try
    while ((bytes = m_channel.read(buffer)) > 0)
    buffer.flip();
    // process buffer
    buffer.clear();
    System.out.println("connection closed");
    m_channel.close();
    catch (IOException e)
    System.out.println("run: " + e.getMessage());
    sm_logger.log(Level.SEVERE, e.getMessage(), e);
    catch (Exception e)
    System.out.println("run: " + e.getMessage());
    sm_logger.log(Level.SEVERE, e.getMessage(), e);
    private SocketChannel m_channel;
    private static Logger sm_logger = Logger.getLogger("MultiThreadServer");
    Client
    import java.io.*;
    import java.net.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.charset.*;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class MultiClient implements Runnable
    public static void main(String[] args) throws Exception
    if (args.length < 1)
    System.out.println("usage: java MultiClient number [host]");
    System.exit(1);
    int number = Integer.parseInt(args[0]);
    String host = (args.length == 2) ? args[1] : "localhost" ;
    Thread[] threads = new Thread [number];
    InetSocketAddress address = new InetSocketAddress(host, 80);
    for (int i = 0; i < number; i++)
    MultiClient client = new MultiClient(address, Integer.toString(i));
    threads[i] = new Thread(client);
    threads.setDaemon(true);
    for (int i = 0; i < number; i++)
    threads[i].start();
    for (int i = 0; i < number; i++)
    threads[i].join();
    public MultiClient(InetSocketAddress address, String id)
    throws InterruptedException, IOException
    m_id = id;
    Charset charset = Charset.forName("UTF-8");
    m_decoder = charset.newDecoder();
    m_encoder = charset.newEncoder();
    m_channel = SocketChannel.open();
    m_channel.connect(address);
    if (id.equals("0"))
    Socket socket = m_channel.socket();
    System.out.println("SO_SNDBUF=" + socket.getSendBufferSize()
    + ",SO_TIMEOUT=" + socket.getSoTimeout()
    + ",SO_KEEPALIVE=" + socket.getKeepAlive());
    byte[] buf = new byte [1024]; // bufsize = 1K
    Arrays.fill(buf, (byte) m_id.charAt(0));
    m_buffer = ByteBuffer.allocateDirect(1024);
    m_buffer.put(buf);
    m_buffer.flip();
    Thread.currentThread().sleep(50L);
    public void run()
    System.out.print(m_id);
    try
    while (true)
    m_channel.write(m_buffer);
    m_buffer.rewind();
    Thread.currentThread().sleep(1000L);
    catch (IOException ioe)
    ioe.printStackTrace();
    catch (InterruptedException ie)
    System.err.println(ie.toString());
    private String m_id;
    private CharsetEncoder m_encoder;
    private CharsetDecoder m_decoder;
    private SocketChannel m_channel;
    private ByteBuffer m_buffer;

    {This is a crosspost. I posted this earlier today at http://forum.java.sun.com/thread.jsp?forum=4&thread=319822 before I stumbled on a search phrase that located this older thread.
    All follow-ups should be on haam's thread instead of mine. The important point below is that NIO select() behavior (vs. threading IO)  is [b]worse under Windows but better under Solaris. This seems fundamentally broken. }
    My company sells a scalable multi-user server platform built on Java 2.
    It runs under Java 1.3.1 (and 1.4.0 windows) using multiple threads for communications, and 1.4.x (unix) using NIO. We were happy to see that 1.4.1 (windows) fixed the problem drastically limiting the number of selectable ports. :-)
    The bad news is that whatever the VM is doing "under the sheets" to fix the problem seems to perform very poorly in terms of CPU:
    I compared connecting 500 simulated users to a Solaris 8 and a Win2K box. These users were in 25 chat rooms, each sending a chat message every 30 seconds. (There was plenty of memory on each machine. There was no swapping in either case. Clock/CPU type doesn't matter as this isn't about comparing a machine to a machine, but different load characteristics -within- a machine environment.)
                    Threaded IO      NIO/Select
    Solaris 1.4.1     20-30%           15- 20%
    Windows 1.4.1     40-50%           95-100%Numbers are % of CPU as reported by 'top' and the Win2K task manager.
    Both platforms showed the expected significant improvement in memory usage when moving from standard threaded IO to NIO.
    Strangely, the Windows implementation of the VM showed a significant (and unexpected) degradation of NIO performance vs. the threaded model, whereas the Solaris VM behaved as expected: NIO outperformed threaded IO.
    Our best guess is that the Selector fix in 1.4.1 is implemented in some cpu-intensive way; perhaps polling. As a result, we still can't use NIO for Wintel boxes running our server. :-( To us, Selector
    is still broken.
    Has anyone else seen results like this? Have we missed some configuration parameter that will fix this?
    I thought the big upside of using select() was supposed to be performance. :-P
    F. Randall Farmer
    State Software, Inc.
    http://www.statesoftware.com

  • Non-blocking SSLEngine example

    Since the example of using SSLEngine with non-blocking IO that comes with Java is quite limited, I have decided to release my own for anyone who wants to see how I solved the various problems that you must face. The example is designed to be a generic non-blocking server that supports SSL.
    This is only meant to be an example, as I wrote this mostly in order to learn how to use the SSLEngine, and therefore has certain limitations, and is not thouroughly tested.
    You can download the file at: http://members.aol.com/ben77/nio_server2.tar.gz
    Here is also the code for SecureIO, which is roughly analagous to the Java example's ChannelIOSecure:
    package nio_server2.internalio;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.*;
    import java.util.concurrent.*;
    import javax.net.ssl.*;
    import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
    * Does IO based on a <code>SocketChannel</code> with all data encrypted using
    * SSL.
    * @author ben
    public class SecureIO extends InsecureIO {
          * SSLTasker is responsible for dealing with all long running tasks required
          * by the SSLEngine
          * @author ben
         private class SSLTasker implements Runnable {
               * @inheritDoc
              public void run() {
                   Runnable r;
                   while ((r = engine.getDelegatedTask()) != null) {
                        r.run();
                   if (inNet.position() > 0) {
                        regnow(); // we may already have read what is needed
                   try {
                        System.out.println(":" + engine.getHandshakeStatus());
                        switch (engine.getHandshakeStatus()) {
                             case NOT_HANDSHAKING:
                                  break;
                             case FINISHED:
                                  System.err.println("Detected FINISHED in tasker");
                                  Thread.dumpStack();
                                  break;
                             case NEED_TASK:
                                  System.err.println("Detected NEED_TASK in tasker");
                                  assert false;
                                  break;
                             case NEED_WRAP:
                                  rereg(SelectionKey.OP_WRITE);
                                  break;
                             case NEED_UNWRAP:
                                  rereg(SelectionKey.OP_READ);
                                  break;
                   } catch (IOException e) {
                        e.printStackTrace();
                        try {
                             shutdown();
                        } catch (IOException ex) {
                             ex.printStackTrace();
                   hsStatus = engine.getHandshakeStatus();
                   isTasking = false;
         private SSLEngine engine;
         private ByteBuffer inNet; // always cleared btwn calls
         private ByteBuffer outNet; // when hasRemaining, has data to write.
         private static final ByteBuffer BLANK = ByteBuffer.allocate(0);
         private boolean initialHandshakeDone = false;
         private volatile boolean isTasking = false;
         private boolean handshaking = true;
         private SSLEngineResult.HandshakeStatus hsStatus = NEED_UNWRAP;
         private boolean shutdownStarted;
         private static Executor executor = getDefaultExecutor();
         private SSLTasker tasker = new SSLTasker();
         private ByteBuffer temp;
          * @return the default <code>Executor</code>
         public static Executor getDefaultExecutor() {
              return new ThreadPoolExecutor(3, Integer.MAX_VALUE, 60L,
                        TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
                        new DaemonThreadFactory());
         private static class DaemonThreadFactory implements ThreadFactory {
              private static ThreadFactory defaultFactory = Executors
                        .defaultThreadFactory();
               * Creates a thread using the default factory, but sets it to be daemon
               * before returning it
               * @param r
               *            the runnable to run
               * @return the new thread
              public Thread newThread(Runnable r) {
                   Thread t = defaultFactory.newThread(r);
                   t.setDaemon(true);
                   return t;
          * @return the executor currently being used for all long-running tasks
         public static Executor getExecutor() {
              return executor;
          * Changes the executor being used for all long-running tasks. Currently
          * running tasks will still use the old executor
          * @param executor
          *            the new Executor to use
         public static void setExecutor(Executor executor) {
              SecureIO.executor = executor;
          * Creates a new <code>SecureIO</code>
          * @param channel
          *            the channel to do IO on.
          * @param sslCtx
          *            the <code>SSLContext</code> to use
         public SecureIO(SocketChannel channel, SSLContext sslCtx) {
              super(channel);
              engine = sslCtx.createSSLEngine();
              engine.setUseClientMode(false);
              int size = engine.getSession().getPacketBufferSize();
              inNet = ByteBuffer.allocate(size);
              outNet = ByteBuffer.allocate(size);
              outNet.limit(0);
              temp = ByteBuffer.allocate(engine.getSession()
                        .getApplicationBufferSize());
         private void doTasks() throws IOException {
              rereg(0); // don't do anything until the task is done.
              isTasking = true;
              SecureIO.executor.execute(tasker);
          * Does all handshaking required by SSL.
          * @param dst
          *            the destination from an application data read
          * @return true if all needed SSL handshaking is currently complete.
          * @throws IOException
          *             if there are errors in handshaking.
         @Override
         public boolean doHandshake(ByteBuffer dst) throws IOException {
              if (!handshaking) {
                   return true;
              if (dst.remaining() < minBufferSize()) {
                   throw new IllegalArgumentException("Buffer has only "
                             + dst.remaining() + " left + minBufferSize is "
                             + minBufferSize());
              if (outNet.hasRemaining()) {
                   if (!flush()) {
                        return false;
                   switch (hsStatus) {
                        case FINISHED:
                             handshaking = false;
                             initialHandshakeDone = true;
                             rereg(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                             return true;
                        case NEED_UNWRAP:
                             rereg(SelectionKey.OP_READ);
                             break;
                        case NEED_TASK:
                             doTasks();
                             return false;
                        case NOT_HANDSHAKING:
                             throw new RuntimeException(
                                       "NOT_HANDSHAKING encountered when handshaking");
              SSLEngineResult res;
              System.out.println(hsStatus + "1" + handshaking);
              switch (hsStatus) {
                   case NEED_UNWRAP:
                        int i;
                        do {
                             rereg(SelectionKey.OP_READ);
                             i = super.read(inNet);
                             if (i < 0) {
                                  engine.closeInbound();
                                  handshaking = false;
                                  shutdown();
                                  return true;
                             if (i == 0 && inNet.position() == 0) {
                                  return false;
                             inloop: do {
                                  inNet.flip();
                                  temp.clear();
                                  res = engine.unwrap(inNet, temp);
                                  inNet.compact();
                                  temp.flip();
                                  if (temp.hasRemaining()) {
                                       dst.put(temp);
                                  switch (res.getStatus()) {
                                       case OK:
                                            hsStatus = res.getHandshakeStatus();
                                            if (hsStatus == NEED_TASK) {
                                                 doTasks();
                                            // if (hsStatus == FINISHED) {
                                            // // if (!initialHandshakeDone) {
                                            // // throw new RuntimeException(hsStatus
                                            // // + " encountered when handshaking");
                                            // initialHandshakeDone = true;
                                            // handshaking=false;
                                            // key.interestOps(SelectionKey.OP_READ
                                            // | SelectionKey.OP_WRITE);
                                            // TODO check others?
                                            break;
                                       case BUFFER_UNDERFLOW:
                                            break inloop;
                                       case BUFFER_OVERFLOW:
                                       case CLOSED:
                                            throw new RuntimeException(res.getStatus()
                                                      + " encountered when handshaking");
                             } while (hsStatus == NEED_UNWRAP
                                       && dst.remaining() >= minBufferSize());
                        } while (hsStatus == NEED_UNWRAP
                                  && dst.remaining() >= minBufferSize());
                        if (inNet.position() > 0) {
                             System.err.println(inNet);
                        if (hsStatus != NEED_WRAP) {
                             break;
                        } // else fall through
                        rereg(SelectionKey.OP_WRITE);
                   case NEED_WRAP:
                        do {
                             outNet.clear();
                             res = engine.wrap(BLANK, outNet);
                             switch (res.getStatus()) {
                                  case OK:
                                       outNet.flip();
                                       hsStatus = res.getHandshakeStatus();
                                       if (hsStatus == NEED_TASK) {
                                            doTasks();
                                            return false;
                                       // TODO check others?
                                       break;
                                  case BUFFER_OVERFLOW:
                                       outNet.limit(0);
                                       int size = engine.getSession()
                                                 .getPacketBufferSize();
                                       if (outNet.capacity() < size) {
                                            outNet = ByteBuffer.allocate(size);
                                       } else { // shouldn't happen
                                            throw new RuntimeException(res.getStatus()
                                                      + " encountered when handshaking");
                                  case BUFFER_UNDERFLOW: // engine shouldn't care
                                  case CLOSED:
                                       throw new RuntimeException(res.getStatus()
                                                 + " encountered when handshaking");
                        } while (flush() && hsStatus == NEED_WRAP);
                        break;
                   case NEED_TASK:
                        doTasks();
                        return false;
                   case FINISHED:
                        break; // checked below
                   case NOT_HANDSHAKING:
                        System.err.println(hsStatus + " encountered when handshaking");
                        handshaking = false;
                        initialHandshakeDone = true;
                        rereg(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
              if (hsStatus == FINISHED) {
                   // if (!initialHandshakeDone) {
                   // throw new RuntimeException(hsStatus
                   // + " encountered when handshaking");
                   initialHandshakeDone = true;
                   handshaking = false;
                   rereg(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
              System.out.println(hsStatus + "2" + handshaking);
              return !handshaking;
          * Attempts to flush all buffered data to the channel.
          * @return true if all buffered data has been written.
          * @throws IOException
          *             if there are errors writing the data
         @Override
         public boolean flush() throws IOException {
              if (!outNet.hasRemaining()) {
                   return true;
              super.write(outNet);
              return !outNet.hasRemaining();
          * @return the largest amount of application data that could be read from
          *         the channel at once.
         @Override
         public int minBufferSize() {
              return engine.getSession().getApplicationBufferSize();
          * Begins or proceeds with sending an SSL shutdown message to the client.
          * @return true if all needed IO is complete
          * @throws IOException
          *             if there are errors sending the message.
         @Override
         public boolean shutdown() throws IOException {
              if (!shutdownStarted) {
                   shutdownStarted = true;
                   engine.closeOutbound();
              if (outNet.hasRemaining() && !flush()) {
                   return false;
              SSLEngineResult result;
              do {
                   outNet.clear();
                   result = engine.wrap(BLANK, outNet);
                   if (result.getStatus() != SSLEngineResult.Status.CLOSED) {
                        throw new IOException("Unexpected result in shutdown:"
                                  + result.getStatus());
                   outNet.flip();
                   if (outNet.hasRemaining() && !flush()) {
                        return false;
              } while (result.getHandshakeStatus() == NEED_WRAP);
              return !outNet.hasRemaining();
          * Reads all possible data into the <code>ByteBuffer</code>.
          * @param dst
          *            the buffer to read into.
          * @return the number of bytes read, or -1 if the channel or
          *         <code>SSLEngine</code> is closed
          * @throws IllegalStateException
          *             if the initial handshake isn't complete *
          * @throws IOException
          *             if there are errors.
          * @throws IllegalStateException
          *             if the initial handshake isn't complete
          * @throws IllegalArgumentException
          *             if the remaining space in dst is less than
          *             {@link SecureIO#minBufferSize()}
         @Override
         public int read(ByteBuffer dst) throws IOException {
              if (!initialHandshakeDone) {
                   throw new IllegalStateException("Initial handshake incomplete");
              if (dst.remaining() < minBufferSize()) {
                   throw new IllegalArgumentException("Buffer has only "
                             + dst.remaining() + " left + minBufferSize is "
                             + minBufferSize());
              int sPos = dst.position();
              int i;
              while ((i = super.read(inNet)) != 0
                        && dst.remaining() >= minBufferSize()) {
                   if (i < 0) {
                        engine.closeInbound();
                        shutdown();
                        return -1;
                   do {
                        inNet.flip();
                        temp.clear();
                        SSLEngineResult result = engine.unwrap(inNet, temp);
                        inNet.compact();
                        temp.flip();
                        if (temp.hasRemaining()) {
                             dst.put(temp);
                        switch (result.getStatus()) {
                             case BUFFER_UNDERFLOW:
                                  continue;
                             case BUFFER_OVERFLOW:
                                  throw new Error();
                             case CLOSED:
                                  return -1;
                             // throw new IOException("SSLEngine closed");
                             case OK:
                                  checkHandshake();
                                  break;
                   } while (inNet.position() > 0);
              return dst.position() - sPos;
          * Encrypts data and writes it to the channel.
          * @param src
          *            the data to write
          * @return the number of bytes written
          * @throws IOException
          *             if there are errors.
          * @throws IllegalStateException
          *             if the initial handshake isn't complete
         @Override
         public int write(ByteBuffer src) throws IOException {
              if (!initialHandshakeDone) {
                   throw new IllegalStateException("Initial handshake incomplete");
              if (!flush()) {
                   return 0;
              int written = 0;
              outer: while (src.hasRemaining()) {
                   outNet.clear(); // we flushed it
                   SSLEngineResult result = engine.wrap(src, outNet);
                   outNet.flip();
                   switch (result.getStatus()) {
                        case BUFFER_UNDERFLOW:
                             break outer; // not enough left to send (prob won't
                        // happen - padding)
                        case BUFFER_OVERFLOW:
                             if (!flush()) {
                                  break outer; // can't remake while still have
                                  // stuff to write
                             int size = engine.getSession().getPacketBufferSize();
                             if (outNet.capacity() < size) {
                                  outNet = ByteBuffer.allocate(size);
                             } else { // shouldn't happen
                                  throw new RuntimeException(hsStatus
                                            + " encountered when handshaking");
                             continue; // try again
                        case CLOSED:
                             throw new IOException("SSLEngine closed");
                        case OK:
                             checkHandshake();
                             break;
                   if (!flush()) {
                        break;
              return written;
         private boolean hasRemaining(ByteBuffer[] src) {
              for (ByteBuffer b : src) {
                   if (b.hasRemaining()) {
                        return true;
              return false;
          * Encrypts data and writes it to the channel.
          * @param src
          *            the data to write
          * @return the number of bytes written
          * @throws IOException
          *             if there are errors.
          * @throws IllegalStateException
          *             if the initial handshake isn't complete
         @Override
         public long write(ByteBuffer[] src) throws IOException {
              if (!initialHandshakeDone) {
                   throw new IllegalStateException("Initial handshake incomplete");
              if (!flush()) {
                   return 0;
              int written = 0;
              outer: while (hasRemaining(src)) {
                   outNet.clear(); // we flushed it
                   SSLEngineResult result = engine.wrap(src, outNet);
                   outNet.flip();
                   switch (result.getStatus()) {
                        case BUFFER_UNDERFLOW:
                             break outer; // not enough left to send (prob won't
                        // happen - padding)
                        case BUFFER_OVERFLOW:
                             if (!flush()) {
                                  break outer; // can't remake while still have
                                  // stuff to write
                             int size = engine.getSession().getPacketBufferSize();
                             if (outNet.capacity() < size) {
                                  outNet = ByteBuffer.allocate(size);
                             } else { // shouldn't happen
                                  throw new RuntimeException(hsStatus
                                            + " encountered when handshaking");
                             continue; // try again
                        case CLOSED:
                             throw new IOException("SSLEngine closed");
                        case OK:
                             checkHandshake();
                             break;
                   if (!flush()) {
                        break;
              return written;
         private void checkHandshake() throws IOException {
              // Thread.dumpStack();
              // System.out.println(engine.getHandshakeStatus());
              outer: while (true) {
                   switch (engine.getHandshakeStatus()) {
                        case NOT_HANDSHAKING:
                             initialHandshakeDone = true;
                             handshaking = false;
                             rereg(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                             return;
                        case FINISHED:
                             // this shouldn't happen, I don't think. If it does, say
                             // where.
                             System.err.println("Detected FINISHED in checkHandshake");
                             Thread.dumpStack();
                             break outer;
                        case NEED_TASK:
                             if (isTasking) {
                                  while (isTasking) { // TODO: deal with by reg?
                                       Thread.yield();
                                       try {
                                            Thread.sleep(1);
                                       } catch (InterruptedException ex) {
                                            // TODO Auto-generated catch block
                                            ex.printStackTrace();
                                  break;
                             doTasks();
                             break;
                        case NEED_WRAP:
                             rereg(SelectionKey.OP_WRITE);
                             break outer;
                        case NEED_UNWRAP:
                             rereg(SelectionKey.OP_READ);
                             break outer;
              handshaking = true;
              hsStatus = engine.getHandshakeStatus();
          * @return true if the channel is open and no shutdown message has been
          *         recieved.
         @Override
         public boolean isOpen() {
              return super.isOpen() && !engine.isInboundDone();
    }

    That's the reason for checkHandshake(), it is called on every read and write and detects a new handshake and configures the setup properly. As far as the server requesting a new handshake, I did not put that in. It would be simple enough though, you would just need to call SSLEngine.beginHandshake() + then call checkHandshake().
    Also, my echo server example had a bug, I forgot to call bu.flip() before queueWrite(), so I fixed that, as well as adding an onConnect method that is called when a connection has been established. The new version should be up at the origional address shortly.

  • Non-blocking SocketChannel and close - huh?

    It looks like closing a socketchannel that is in non-blocking mode can result in a dead drop of the connection, with some bytes that have already been sent and accepted (as in, 'consumed' from the buffer) being completely dropped.
    In fact, I'm generally confused: Actual C non-blocking code has a similar setup for 'close' as you can see in SocketChannel's OP_CONNECT behaviour - just because you want to connect the socket doesn't mean it magically happends without blocking. Wait for a ready flag, then try again.
    It should work the same way with close (you try to close, but it may not be possible without blocking).
    Is this a huge gaping bug that no one quite figured out just yet, or did I miss something? I loathe to turn on linger, as that's just a crapshoot (you never know if it actually gets through. You could run into funny surprises once the server gets a bit busy. I'd rather not) and tends to cause massive leaks on linux, at least according to some google searches.
    There seems to be slightly better performance (in that I have always received all data sofar) if I close the socket instead of the channel (socketChannel.socket().close() instead of socketChannel.close()) - but this has been a random attempt at 'making it work', and I can't find any documentation that backs up that this will DEFINITELY not lose any information without letting me know somehow. That still sounds impossible with this approach.

    Actual C non-blocking code has a similar setup for
    'close' as you can see in SocketChannel's
    OP_CONNECT behaviour ...No it doesn't. I don't know what you mean by this.
    Closing a socket is asynchronous, but it shouldn't lose any data - if the data can be delivered it will be, followed by the FIN. You don't know when it is delivered, and you don't get to hear about any errors such as an RST coming from the other end, say if it decided to close before reading all the data, or if some intermediate router hung you up.
    I'm wondering if you are really dealing with all the short write and zero length write possibilities, i.e. whether the data isn't really still in your output buffer. Don't wish to teach you to suck eggs but there are some subtleties here.
    Setting a positive linger timeout doesn't really help because Java doesn't tell you if the timeout expired. (I only asked for this about four years ago). You get to wait while any pending data is delivered, but you still have to guess about whether it all went or the timeout expired, and the behaviour after the timeout expires is platform-dependent: some (Windows) issue an RST, others (Unix) keep trying.
    Blocking or non-blocking mode shouldn't make any difference to this (except that Linux will block on a positive linger timeout even in non-blocking mode, which is wrong), and whether you close the channel or the socket is immaterial as one calls the other anyway.
    The reason why OP_CONNECT is different in blocking/non-blocking modes is that in blocking mode it won't return until the SYN-ACK is received, while in non-blocking mode it just sends the SYN and relies on you calling connect() again (at the C level) to collect the SYN-ACK, or not - this is what finishConnect() tells you.
    tends to cause massive leaks on linux, at least
    according to some google searchesIt can't, unless you are referring to the Helix client thing, which appears to be just a badly designed C++ class library with, for some reason, its own linger implementation outside the kernel.

  • FileChannel.transferTo() using non-blocking SocketChannel

    I'm looking to use FileChannel.transfer(From/To) for performing file transfers over a network. On the uploading side I find that even though I set the SocketChannel to non-blocking mode, the loop manages to send files as large as 30MB in only a single transferTo() invocation. What I'm hoping for is to have a series of partial writes so that I might generate progress notifications, which does occur on the downloading side with transferFrom(). I don't think a file that large should be transferred in one pass so I'm thinking that this is caused by either some internal buffer allocation issue or a goof-up on my part.
    Thanks in advance for any input and here's the uploading code:
    public void upload( String fileName ) {
    FileInputStream fileStrm = null;
    FileChannel fileChan = null;
    FileLock fileLock = null;
    SocketChannel sockChan = null;
    Selector selector = null;
    try {
    // Lock the source file.
    file = new File( fileName );
    fileStrm = new FileInputStream( file );
    fileChan = fileStrm.getChannel();
    fileLock = fileChan.lock( 0L, Long.MAX_VALUE, true );
    // Open a server socket bound to an arbitrary port.
    servChan = ServerSocketChannel.open();
    servChan.socket().bind( new InetSocketAddress( 0 ) );
    // Wait for a single connection and close the server socket.
    sockChan = servChan.accept();
    servChan.close();
    sockChan.configureBlocking( false );
    selector = Selector.open();
    SelectionKey selectorKey = sockChan.register( selector, SelectionKey.OP_WRITE );
    // Loop until transfer has completed.
    int fileSize = ( int ) file.length();
    int sizeLeft = fileSize;
    while ( sizeLeft > 0 ) {
    // Wait for data to read, then transfer it.
    if ( selector.select() == 1 && selectorKey.isWritable() ) {
    sizeLeft -= ( int ) fileChan.transferTo( fileSize - sizeLeft, sizeLeft, sockChan );
    // Generate a progress notification here such as:
    // monitor.bytesTransferred( fileSize - sizeLeft );
    catch ( IOException ex ) {
    ex.printStackTrace();
    finally {
    try {
    // Cleanup.
    if ( selector != null )
    selector.close();
    if ( sockChan != null )
    sockChan.close();
    if ( fileLock != null )
    fileLock.release();
    if ( fileChan != null )
    fileChan.close();
    if ( fileStrm != null )
    fileStrm.close();
    catch ( IOException ex ) {
    ex.printStackTrace();
    -Edwin

    Actually, the sending process appears way ahead of the receiving one, where the send seems to finish in a blink while the receive takes several seconds to complete. In other words, the transferTo() completes in one loop, while the transferFrom() performs many with delays in between. I'd guess all that data is sitting in some large outbound buffer at the sender, which would explain why it seems to finish too quickly. If I split the send into smaller chunks, the two sides run quite nearly at the same pace as expected.
    The receiver already sleeps by way of a select() against a non-blocking SocketChannel, so I'll try reducing the buffer sizes as you suggest.

  • NIO: Strange problem when using ByteBuffer with non-blocking SocketChannel

    Hi,
    I have a server that uses multiplexed, non-blocking I/O with java.nio. When a client connects, the server waits for the message: <system cmd="knock"/>, returns a message and disconnects the client. The clients are shortly serviced in less than a second.
    But the server newer receive anything from about 20% of the clients - even though it is sent. Or with other words: it is received and the data is contained in the ByteBuffer - SocketChannel.read(ByteBuffer) - but a call to ByteBuffer.remaing() returns 0 !!
    ByteBuffer receiveBuf = ByteBuffer.allocate(65536);
    receiveBuf.clear(); // the code is elsewhere used for longer living clients
    int readBytes = channel.read(receiveBuf);
    receiveBuf.flip();
    StringBuffer sb = new StringBuffer();
    System.out.println(" * Remaining: "+receiveBuf.remaining()); // writes: ' * Remaining: 0'
    System.out.println(" * Received: "+new String(receiveBuf.array())); // writes: ' * Received: <system cmd="knock"/>'
    while(receiveBuf.remaining() >= 2) {
      byte b = receiveBuf.get();
      sb.append((char)b);
    System.out.println(" * sb content: "+sb.toString()); // writes: ' * sb content: 'The ByteBuffer clearly receives the correct data, but the ByteBuffer.remaining() returns 0 and therefore the StringBuffer will never have any content.
    The problem seems to occur randomly and for about 20% of the clients (simulated from the same computer and therefore has the same bandwidth and so on).
    Anyone knows what is going on, and how to solve the problem !?

    It's always possible in any read that the number of bytes read is less than the number of bytes requested. You need to keep reading until you have got everything you expected, and cope with every possible error condition on each iteration.
    EJP

  • Non-blocking SocketChannels

    I'm trying to learn how to use non-blocking socket channles, but I haven't found much info (nor luck) so far.
    To learn, I'm building a server and a client. The server accepts input from the clients and process it in only one thread. The clients should send Objects to the server, and the server process them and return the result also as an Object. For this, I'm trying to use ObjectOutputStream and ObjectInputStream.
    The problem I don't know how to solve is that the SocketChannel is in non-bolcking mode, so I can't use their input/output streams (I get a IllegalBlockingModeException). In the server process loop I can reconfigure the SocketChannel to blocking mode to be able to read the Object, but I can't configure it to non-blocking mode again because I get a CancelledKeyException.
    Does anyone know how to work with InputStreams and non-blocking channels? Or where to find more info about it?
    Here are the relevant part of the server code:
    Set ready = selector.selectedKeys();
    Iterator i = ready.iterator();
    while (i.hasNext()) {
       try {
          SelectionKey sk = i.next();
          i.remove();
          if (sk.isAcceptable()) {
             ServerSocketChannel ssc = (ServerSocketChannel)sk.channel();
             SocketChannel sc = ssc.accept();
             sc.configureBlocking(false);
             sc.register(selector, SelectionKey.OP_READ);
          } else if (sk.isReadable()) {
             SocketChannel sc = (SocketChannel)sk.channel();
             // PROBLEM: If the channel is in non-blocking mode
             // I cannot use InputStreams
             sk.cancel();
             sc.configureBlocking(true);
             // Read the object sent by the client
             ObjectInputStream in = new ObjectInputStream(Channels.newInputStream(sc));
             Object o = in.readObject();
             // PROBLEM: Can't put the channel back to non-blocking mode
             sc.configureBlocking(false);
             sc.register(selector, SelectionKey.OP_READ); // CancelledKeyException
       } catch (...){
    }

    In my client, this is working fine:
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    for (int i = 0; i < 30000; i++) {
       oos = new ObjectOutputStream(sc.socket().getOutputStream());
       oos.writeObject(object);
       oos.flush();
       ois = new ObjectInputStream(sc.socket().getInputStream());
       Object o = ois.readObject();
    }But trying to do it like this throws a StreamCorruptedException at the server side.
    ObjectOutputStream oos = new ObjectOutputStream(sc.socket().getOutputStream());
    ObjectInputStream ois = new ObjectInputStream(sc.socket().getInputStream());
    for (int i = 0; i < 30000; i++) {
       oos.writeObject(object);
       oos.flush();
       Object o = ois.readObject();
    }Do you know why?

  • Non-Blocking SocketChannel read give Connection timed out

    Hi,
    My program is using Non-Block NIO SocketChannel for both Server and Client sides. However, if the connection between server and client has been idle for a while, when the client try to communicate with server, it gets blocked for a while (15 minutes) and then receives the following exception:
    java.io.IOException: Connection timed out
         at sun.nio.ch.FileDispatcher.read0(Native Method)
         at sun.nio.ch.SocketDispatcher.read(Unknown Source)
         at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
         at sun.nio.ch.IOUtil.read(Unknown Source)
         at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
    How can this be since the read is in Non-Blocking mode? Also, is there anyway to determine the timeout without being blocked?

    This would mean that you are trying to read from the socket without having properly completed the connection, which timed out. I would say you are connecting in non-blocking mode but not calling finishConnect() when you get OP_CONNECT.

  • NIO SocketChannel non-blocking read

    Hello.
    I'm not sure a resembling message has already been posted in a forum. In such a case, thanks for redirecting to it.
    Goals :
    A selector is used by a main server thread to make accept and read operations non-blocking.
    When a connection is accepted, the newly created socket channel is configured as non-blocking, and a key is added to the selector with the OP_READ flag.
    When data are available on sockets, a new task (runnable) is created and submitted to a thread pool.
    When a thread is ready to process the request, a pre-allocated bytebuffer is used to read arriving data.
    The problem :
    When the bytebuffer capacity is less than the received bytes count, the read method on the socket channel interrupts the selector on the same selection key. In response to this event, a new task is initiated, interferring with the previous one.
    As expected according to my concurrency policy (ie. with a pool of threads), several requests are processed by parallel threads. To avoid unsafe accesses, i added a synchronized statement on the channel blocking lock, and it seems to works fine. But a better way should exist...
    Questions :
    Is this the expected behavior ?
    Is there any other way to read received data with a small buffer ?
    The full copy of the source code :
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.channels.spi.*;
    import java.nio.charset.*;
    import java.net.*;
    import java.util.*;
    import net.moon.threads.*;
    public class Nio1 {
         static class Request {
              boolean isCompleted = false;
              int inputs = 0;
              Set workers = new HashSet();
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              byte p = 0;
              boolean isCompleted() {
                   return isCompleted;
              void countInput() {
                   inputs++;
              void append(final ByteBuffer byteBuffer) {
                   if (isCompleted)
                        throw new IllegalStateException("Request is already completed");
                   workers.add(Thread.currentThread());
                   while (byteBuffer.hasRemaining()) {
                        byte b = byteBuffer.get();
                        baos.write(b);
                        if ((b == '\r') && (p == '\n'))
                             isCompleted = true;
                        p = b;
              int inputs() {
                   return inputs;
              Thread[] workers() {
                   return (Thread[]) workers.toArray(new Thread[0]);
              int size() {
                   return baos.size();
              byte[] getData() {
                   return baos.toByteArray();
              void reset() {
                   isCompleted = false;
                   inputs = 0;
                   baos.reset();
                   workers.clear();
         static private class RequestTask implements Runnable {
         private final static Charset charset = Charset.forName("US-ASCII");
              private final Server server;
              private final SelectionKey selectionKey;
              RequestTask(final Server server, final SelectionKey selectionKey) {
                   this.server = server;
                   this.selectionKey = selectionKey;
              public void run() {
                   log("*** Processing input...");
                   try {
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
    synchronized(channel.blockingLock()) {
                        Request request = (Request) selectionKey.attachment();
                        request.countInput();
                        State state = getState();
                        log("Reading first...");
                        int c = channel.read(state.byteBuffer);
                        log("... Read first : " + c);
                        if (c > 0) {
                             for(;;) {
                                  state.byteBuffer.flip();
                             request.append(state.byteBuffer);
                                  state.byteBuffer.clear();
                                  if (c < state.byteBuffer.capacity()) break;
                                  log("Reading next...");
                                  c = channel.read(state.byteBuffer);
                                  log("... Read next : " + c);
                                  if (c <= 0) break;
                             if (request.isCompleted()) {
                                  log("Request completed : " + request.inputs());
                                  StringBuffer bodyBuffer = new StringBuffer();
                                  bodyBuffer.append("-----------------------------\r\n");
                                  bodyBuffer.append("Request processed in " + request.inputs() + " inputs\r\n");
                                  bodyBuffer.append("Request size is " + request.size() + " bytes\r\n");
                                  bodyBuffer.append("Participating workers :\r\n");
                                  Thread[] workers = request.workers();
                                  for (int i = 0; i < workers.length; i++)
                                       bodyBuffer.append(" * " + workers[i] + "\r\n");
                                  bodyBuffer.append("-----------------------------\r\n");
                                  StringBuffer headerBuffer = new StringBuffer();
                                  headerBuffer.append("HTTP/1.1 200 OK\r\n");
                                  headerBuffer.append("Server: NIO Server 1\r\n");
                                  headerBuffer.append("Content-Type: text/plain\r\n");
                                  headerBuffer.append("Content-Length: ").append(request.size() + bodyBuffer.length()).append("\r\n");
                                  headerBuffer.append("\r\n");
                             CharsetEncoder encoder = charset.newEncoder();
                                  channel.write(encoder.encode(CharBuffer.wrap(headerBuffer)));
                                  channel.write(encoder.encode(CharBuffer.wrap(bodyBuffer)));
                                  channel.write(ByteBuffer.wrap(request.getData()));
                                  request.reset();
                        if (c < 0) {
                             selectionKey.attach(null);
                             selectionKey.cancel();
                             log("!!! Connection terminated for channel " + channel);
                   catch(final Exception x) {
                        x.printStackTrace();
                   log("*** Request processed...");
              private State getState() {
                   State state = (State) server.taskManager.getCurrentWorkerState();
                   if (state == null) {
                        state = new State();
                        server.taskManager.setCurrentWorkerState(state);
                   else {
                        state.byteBuffer.clear();
                   return state;
              private void log(final String text) {
                   System.out.println(Thread.currentThread() + " : " + text);
              static class State {
                   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(32);
         static private class Server implements Runnable {
              private final int port;
              private Thread worker;
              private FIFOTaskManager taskManager;
              Server(final int port) {
                   this.port = port;
                   worker = null;
              synchronized void start() throws Exception {
                   if (worker == null) {
                        log("Starting the server...");
                        taskManager = new FIFOTaskManager("Nio1Workers", 24);
                        worker = new Thread(this);
                        worker.start();
                        synchronized(worker) {
                             try {
                                  worker.wait();
                             catch(InterruptedException x) {
                        log("Server started !");
              public void run() {
                   try {
                        log("Server is starting...");
                        Selector selector = SelectorProvider.provider().openSelector();
                        log("Creating listener on port " + port);
                        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
                        serverSocketChannel.configureBlocking(false);
                        InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
                        serverSocketChannel.socket().bind(inetSocketAddress);
                        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
                        synchronized(worker) {
                             worker.notify();
                        while (selector.select() >= 0) {
                             Set readyKeys = selector.selectedKeys();
                             log("Keys are ready : " + readyKeys.size());
                             for (Iterator i = readyKeys.iterator(); i.hasNext(); ) {
                                  SelectionKey selectedKey = (SelectionKey) i.next();
                                  if (selectedKey.isAcceptable()) {
                                       ServerSocketChannel ssc = (ServerSocketChannel) selectedKey.channel();
                                       SocketChannel sc = ssc.accept();
                                       sc.configureBlocking(false);
                                       SelectionKey sk = sc.register(selector, SelectionKey.OP_READ);
                                       sk.attach(new Request());
                                       log("Connection accepted for channel " + sc);
                                  else if (selectedKey.isReadable()) {
                                       log("Key ready for input : " + selectedKey);
                                       taskManager.execute(new RequestTask(this, selectedKey));
                                  i.remove();
                             readyKeys = null;
                        log("Server loop interrupted !");
                   catch(Exception x) {
                        x.printStackTrace();
              private void log(final String text) {
                   System.out.println("SERVER: " + text);
         public static void main(final String[] args) throws Exception {
              Server server = new Server(9001);
              server.start();

    Thanks for the trick. I hope the code will be more readable than my sockets !
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.channels.spi.*;
    import java.nio.charset.*;
    import java.net.*;
    import java.util.*;
    import net.moon.threads.*;
    public class Nio1 {
         static class Request {
              boolean isCompleted = false;
              int inputs = 0;
              Set workers = new HashSet();
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              byte p = 0;
              boolean isCompleted() {
                   return isCompleted;
              void countInput() {
                   inputs++;
              void append(final ByteBuffer byteBuffer) {
                   if (isCompleted)
                        throw new IllegalStateException("Request is already completed");
                   workers.add(Thread.currentThread());
                   while (byteBuffer.hasRemaining()) {
                        byte b = byteBuffer.get();
                        baos.write(b);
                        if ((b == '\r') && (p == '\n'))
                             isCompleted = true;
                        p = b;
              int inputs() {
                   return inputs;
              Thread[] workers() {
                   return (Thread[]) workers.toArray(new Thread[0]);
              int size() {
                   return baos.size();
              byte[] getData() {
                   return baos.toByteArray();
              void reset() {
                   isCompleted = false;
                   inputs = 0;
                   baos.reset();
                   workers.clear();
         static private class RequestTask implements Runnable {
             private final static Charset charset = Charset.forName("US-ASCII");
              private final Server server;
              private final SelectionKey selectionKey;
              RequestTask(final Server server, final SelectionKey selectionKey) {
                   this.server = server;
                   this.selectionKey = selectionKey;
              public void run() {
                   log("*** Processing input...");
                   try {
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
    synchronized(channel.blockingLock()) {
                        Request request = (Request) selectionKey.attachment();
                        request.countInput();
                        State state = getState();
                        log("Reading first...");
                        int c = channel.read(state.byteBuffer);
                        log("... Read first : " + c);
                        if (c > 0) {
                             for(;;) {
                                  state.byteBuffer.flip();
                                 request.append(state.byteBuffer);
                                  state.byteBuffer.clear();
                                  if (c < state.byteBuffer.capacity()) break;
                                  log("Reading next...");
                                  c = channel.read(state.byteBuffer);
                                  log("... Read next : " + c);
                                  if (c <= 0) break;
                             if (request.isCompleted()) {
                                  log("Request completed : " + request.inputs());
                                  StringBuffer bodyBuffer = new StringBuffer();
                                  bodyBuffer.append("-----------------------------\r\n");
                                  bodyBuffer.append("Request processed in " + request.inputs() + " inputs\r\n");
                                  bodyBuffer.append("Request size is " + request.size() + " bytes\r\n");
                                  bodyBuffer.append("Participating workers :\r\n");
                                  Thread[] workers = request.workers();
                                  for (int i = 0; i < workers.length; i++)
                                       bodyBuffer.append(" * " + workers[i] + "\r\n");
                                  bodyBuffer.append("-----------------------------\r\n");
                                  StringBuffer headerBuffer = new StringBuffer();
                                  headerBuffer.append("HTTP/1.1 200 OK\r\n");
                                  headerBuffer.append("Server: NIO Server 1\r\n");
                                  headerBuffer.append("Content-Type: text/plain\r\n");
                                  headerBuffer.append("Content-Length: ").append(request.size() + bodyBuffer.length()).append("\r\n");
                                  headerBuffer.append("\r\n");
                                 CharsetEncoder encoder = charset.newEncoder();
                                  channel.write(encoder.encode(CharBuffer.wrap(headerBuffer)));
                                  channel.write(encoder.encode(CharBuffer.wrap(bodyBuffer)));
                                  channel.write(ByteBuffer.wrap(request.getData()));
                                  request.reset();
                        if (c < 0) {
                             selectionKey.attach(null);
                             selectionKey.cancel();
                             log("!!! Connection terminated for channel " + channel);
                   catch(final Exception x) {
                        x.printStackTrace();
                   log("*** Request processed...");
              private State getState() {
                   State state = (State) server.taskManager.getCurrentWorkerState();
                   if (state == null) {
                        state = new State();
                        server.taskManager.setCurrentWorkerState(state);
                   else {
                        state.byteBuffer.clear();
                   return state;
              private void log(final String text) {
                   System.out.println(Thread.currentThread() + " : " + text);
              static class State {
                   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(32);
         static private class Server implements Runnable {
              private final int port;
              private Thread worker;
              private FIFOTaskManager taskManager;
              Server(final int port) {
                   this.port = port;
                   worker = null;
              synchronized void start() throws Exception {
                   if (worker == null) {
                        log("Starting the server...");
                        taskManager = new FIFOTaskManager("Nio1Workers", 24);
                        worker = new Thread(this);
                        worker.start();
                        synchronized(worker) {
                             try {
                                  worker.wait();
                             catch(InterruptedException x) {
                        log("Server started !");
              public void run() {
                   try {
                        log("Server is starting...");
                        Selector selector = SelectorProvider.provider().openSelector();
                        log("Creating listener on port " + port);
                        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
                        serverSocketChannel.configureBlocking(false);
                        InetSocketAddress inetSocketAddress = new InetSocketAddress(port);
                        serverSocketChannel.socket().bind(inetSocketAddress);
                        SelectionKey selectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
                        synchronized(worker) {
                             worker.notify();
                        while (selector.select() >= 0) {
                             Set readyKeys = selector.selectedKeys();
                             log("Keys are ready : " + readyKeys.size());
                             for (Iterator i = readyKeys.iterator(); i.hasNext(); ) {
                                  SelectionKey selectedKey = (SelectionKey) i.next();
                                  if (selectedKey.isAcceptable()) {
                                       ServerSocketChannel ssc = (ServerSocketChannel) selectedKey.channel();
                                       SocketChannel sc = ssc.accept();
                                       sc.configureBlocking(false);
                                       SelectionKey sk = sc.register(selector, SelectionKey.OP_READ);
                                       sk.attach(new Request());
                                       log("Connection accepted for channel " + sc);
                                  else if (selectedKey.isReadable()) {
                                       log("Key ready for input : " + selectedKey);
                                       taskManager.execute(new RequestTask(this, selectedKey));
                                  i.remove();
                             readyKeys = null;
                        log("Server loop interrupted !");
                   catch(Exception x) {
                        x.printStackTrace();
              private void log(final String text) {
                   System.out.println("SERVER: " + text);
         public static void main(final String[] args) throws Exception {
              Server server = new Server(9001);
              server.start();
    }

Maybe you are looking for

  • Vga monitor

    I would like to know how can I connect a VGA video projector to a MacBook Air. Through the Mini DisplayPort-VGA adaptor? (using the thunderbolt port?) Tks.

  • Save Copy to Complete Later

    Any suggestions on how to create forms that allow users this option? What I'm experiencing so far is that Acrobat either saves the form as an unfillable PDF, or saves the fillable form without any of the data the user has entered... how does one save

  • Help abt store and delete images as blob & bfile using Visual C++

    Hi Does anyone knows if there is code for visual c++ to store and delete images as blob or bfile? thanx

  • File adapter-File to database insertion -multiple records

    Hi 1)read from a file(3 rows) 2)Transform it 3)invoke Database partner link and insert in a table. 4)assign say an ID. 5)transform 6)Invoke another database partrner and insert in another database *** we are inserting 3 records in the text file being

  • Can't add cellular data

    I want to add cellular data to my ipad(3rd gen) with verizon.I tried to reset reprovision account, reset account,turn on, off, everything, but when I go to my cellular data account I can't connect and comes a message wich says: connection failed, tra