Selectable SocketChannels  or httpurlconnection s?

I?m developing a multithreaded server for sending sms over http.
This will involve various message payloads of various types files / texts etc
Sent to multiple different http destinations via post / get requests.
The system will have to degrade gracefully and respond intelligently to events such as destinations not being reachable, message loads increasing and latency increasing on requests, server 500 http rersponses and 401 not authorised etc.
Ideally I?d like to have the threads use form of ?sleep until I get a response / connection from the remote http server? as this would be optimal.
Currently I?m considering using either
new HttpUrlConnections per message sent
Or
Selectable SocketChannels
As they offer what looks like the type of blocking action I?m looking for.
Can anyone enlighten me as to which method of connecting would give optimal performance and how best to go about it.
Also is HttpUrlConnection thread safe if I create a new instance per message per thread?
I?ve read Doug Lea?s book and have a fair idea of how I?m going to do the threading its just the connection part that I?m wary of.
Thanks,
AnRonMor

I'd recommend the HttpURLConnection. It provides a number of useful methods for dealing with Http and if you plan to use one thread per connection, its fine. I'd use queues between the connection code and the mainline code to avoid any threading issues. Each connection will block independently.
If you use the NIO SelectableChannel you should use a single thread to perform all the i/o. At present the jury is still out on whether the cost of demultiplexing selections is less than the cost of multiple threads. Here's an interesting disussion of NIO Performance, also my thread Taming the NIO Circus provides examples of using NIO, including the queuing of interestOps as suggested in the other thread.

Similar Messages

  • Java.nio select() method return 0 in my client application

    Hello,
    I'm developing a simple chat application who echo messages
    But my client application loop because the select() method return 0
    This is my code
    // SERVER
    package test;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
    public class Server {
         private int port = 5001;
         public void work() {               
              try {
                   ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
                   serverSocketChannel.configureBlocking(false);
                   InetSocketAddress isa = new InetSocketAddress(port);               
                   serverSocketChannel.socket().bind(isa);
                   Selector selector = Selector.open();
                   serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
                   System.out.println("Listing on "+port);
                   while(selector.select()>0) {
                        Set keys = selector.selectedKeys();
                        for(Iterator i = keys.iterator(); i.hasNext();) {
                             SelectionKey key = (SelectionKey) i.next();
                             i.remove();
                             if (key.isAcceptable()) {
                                  ServerSocketChannel keyChannel = (ServerSocketChannel)key.channel();                              
                                  SocketChannel channel = keyChannel.accept();
                                  channel.configureBlocking(false);                              
                                  channel.register(selector, SelectionKey.OP_READ );
                             } else if (key.isReadable()) {
                                  SocketChannel keyChannel = (SocketChannel) key.channel();
                                  String m = Help.read(keyChannel );
                                  Help.write(m.toUpperCase(), keyChannel );
              } catch (IOException e) {                                             
                   e.printStackTrace();                         
         public static void main(String[] args) {
              Server s = new Server();
              s.work();
    // CLIENT
    package test;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    public class Client extends JFrame  {
         private String host = "localhost";
         private int port = 5001;
         private SocketChannel socketChannel;
         private Selector selector;
         public void work() {               
              try {
                   socketChannel = SocketChannel.open();
                   socketChannel.configureBlocking(false);
                   InetSocketAddress isa = new InetSocketAddress(host, port);               
                   socketChannel.connect(isa);
                   selector = Selector.open();
                   socketChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ );
                   while(true) {
                        selector.select();
                        Set keys = selector.selectedKeys();
                        for(Iterator i = keys.iterator(); i.hasNext();) {
                             SelectionKey key = (SelectionKey) i.next();
                             i.remove();
                             if (key.isConnectable()) {
                                  SocketChannel keyChannel = (SocketChannel) key.channel();
                                  if (keyChannel.isConnectionPending()) {
                                       System.out.println("Connected "+keyChannel.finishConnect());                                                                           
                             } else if (key.isReadable()) {                                                                                                                                                           
                                  SocketChannel keyChannel = (SocketChannel) key.channel();                                             
                                  String m = Help.read(keyChannel);
                                  display(m);                                                                                                                                                                                                                   
              } catch (IOException e) {                                             
                   e.printStackTrace();                         
         private void display(final String m) {
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        area.append(m+"\n");
                        textFieed.setText("");
         private void sendMessage(final String m) {
              Thread t = new Thread(new Runnable() {               
                   public void run() {                                                                                
                        try {                         
                             Help.write(m, socketChannel);
                        } catch (IOException e) {               
                             e.printStackTrace();
              t.start();                    
         public Client() {
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(1);
              textFieed.addKeyListener(new KeyAdapter() {
                   public void keyPressed(KeyEvent e) {
                        if (e.getKeyCode()== KeyEvent.VK_ENTER) {
                             String m = textFieed.getText();
                             sendMessage(m);     
              area.setEditable(false);
              getContentPane().add(textFieed, "North");
              getContentPane().add(new JScrollPane(area));
              setBounds(200, 200, 400, 300);
              show();
         private String messageToSend;
         private JTextArea area = new JTextArea();
         JTextField textFieed = new JTextField();
         public static void main(String[] args) {
              Client s = new Client();
              s.work();
    // HELPER CLASS
    package test;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.channels.SocketChannel;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.nio.charset.CharsetEncoder;
    public class Help {
         private static Charset charset = Charset.forName("us-ascii");
         private static CharsetEncoder enc = charset.newEncoder();
         private static CharsetDecoder dec = charset.newDecoder();
         private static void log(String m) {
              System.out.println(m);
         public static String read(SocketChannel channel) throws IOException {
              log("*** start READ");                              
              int n;
              ByteBuffer buffer = ByteBuffer.allocate(1024);
              while((n = channel.read(buffer)) > 0) {
                   System.out.println("     adding "+n+" bytes");
              log("  BUFFER REMPLI : "+buffer);
              buffer.flip();               
              CharBuffer cb = dec.decode(buffer);          
              log("  CHARBUFFER : "+cb);
              String m = cb.toString();
              log("  MESSAGE : "+m);          
              log("*** end READ");
              //buffer.clear();
              return m;                    
         public static void write(String m, SocketChannel channel) throws IOException {          
              log("xxx start WRITE");          
              CharBuffer cb = CharBuffer.wrap(m);
              log("  CHARBUFFER : "+cb);          
              ByteBuffer  buffer = enc.encode(cb);
              log("  BUFFER ALLOUE REMPLI : "+buffer);
              int n;
              while(buffer.hasRemaining()) {
                   n = channel.write(buffer);                         
              System.out.println("  REMAINING : "+buffer.hasRemaining());
              log("xxx end WRITE");

    Here's the fix for that old problem. Change the work method to do the following
    - don't register interest in things that can't happen
    - when you connect register based on whether the connection is complete or pending.
    - add the OP_READ interest once the connection is complete.
    This doesn't fix all the other problems this code will have,
    eg.
    - what happens if a write is incomplete?
    - why does my code loop if I add OP_WRITE interest?
    - why does my interestOps or register method block?
    For code that answers all those questions see my obese post Taming the NIO Circus
    Here's the fixed up Client code
    // CLIENT
    package test
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    public class Client extends JFrame  {
         private String host = "localhost";
         private int port = 5001;
         private SocketChannel socketChannel;
         private Selector selector;
         public void work() {
              try {
                   socketChannel = SocketChannel.open();
                   socketChannel.configureBlocking(false);
                   InetSocketAddress isa = new InetSocketAddress(host, port);
                   socketChannel.connect(isa);
                   selector = Selector.open();
                   int interest = 0;
                   if(socketChannel.isConnected())interest = SelectionKey.OP_READ;
                   else if(socketChannel.isConnectionPending())interest = SelectionKey.OP_CONNECT;
                   socketChannel.register(selector, interest);
                   while(true)
                        int nn = selector.select();
                        System.out.println("nn="+nn);
                        Set keys = selector.selectedKeys();
                        for(Iterator i = keys.iterator(); i.hasNext();)
                             SelectionKey key = (SelectionKey) i.next();
                             i.remove();
                             if (key.isConnectable())
                                  SocketChannel keyChannel = (SocketChannel) key.channel();
                                  System.out.println("Connected "+keyChannel.finishConnect());
                                  key.interestOps(SelectionKey.OP_READ);
                             if (key.isReadable())
                                  SocketChannel keyChannel = (SocketChannel) key.channel();
                                  String m = Help.read(keyChannel);
                                  display(m);
              } catch (IOException e) {
                   e.printStackTrace();
         private void display(final String m) {
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        area.append(m+"\n");
                        textFieed.setText("");
         private void sendMessage(final String m) {
              Thread t = new Thread(new Runnable() {
                   public void run() {
                        try {
                             Help.write(m, socketChannel);
                        } catch (IOException e) {
                             e.printStackTrace();
              t.start();
         public Client() {
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(1);
              textFieed.addKeyListener(new KeyAdapter() {
                   public void keyPressed(KeyEvent e) {
                        if (e.getKeyCode()== KeyEvent.VK_ENTER) {
                             String m = textFieed.getText();
                             sendMessage(m);
              area.setEditable(false);
              getContentPane().add(textFieed, "North");
              getContentPane().add(new JScrollPane(area));
              setBounds(200, 200, 400, 300);
              show();
         private String messageToSend;
         private JTextArea area = new JTextArea();
         JTextField textFieed = new JTextField();
         public static void main(String[] args) {
              Client s = new Client();
              s.work();

  • NIO Socket implementation - delay between select and get data from socket

    Hi all,
    I have implemented a internal CallAPI for RPC over a socket connection. It works fine but if there are more than five clients and some load I have the phenomena that the READ selector returns a SelectorKey but I did not get any data from the socket.
    My implementation is based on NIO has following components:
    + Accept-Thread
    Thread handles new clients.
    + Read-Thread
    Thread handles the data from the socket for the registered client. Each request is handled in an own Process-Thread. A Thread-Pool implementation is used for processing.
    + Process-Thread
    The Process-Thread reads the data from the socket and starts the processing of the logical request.
    In my tests I get the notification of data at the socket. The Process-Thread want to read the data for the socket, but no data are available. In some situations if have to read about 20 times and more to get the data. Between each read attempt I have inserted a sleep in the Process-Thread if no data was available. This have improved the problem, but it already exists. I tested the problem with several systems and jvm's but it seams that it is independent from the system.
    What can I to do improve the situation?
    I already include the read implementation from the grizzly-Framework. But it doesn't improve the situation.
    Socket - Init
         protected void openSocket( String host, int port ) throws IOException
              serverChannel = ServerSocketChannel.open();
              serverChannel.configureBlocking( false );
              serverSocket = serverChannel.socket();
              serverSocket.setReuseAddress( true );
              this.serverhost = host;
              this.serverport = port;
              this.srvAcceptSelector = Selector.open();
              this.srvReadSelector = Selector.open();
              InetSocketAddress isa = null;
              if ( serverhost != null )
                   isa = new InetSocketAddress( this.serverhost, this.serverport );
              else
                   isa = new InetSocketAddress( this.serverport );
              serverSocket.bind( isa, 50 );
              serverChannel.register( this.srvAcceptSelector, SelectionKey.OP_ACCEPT );
         }New Client � Init
         // New Client
         if ( key.isAcceptable())
              keyCountConnect++;
              ServerSocketChannel actChannel =
                   (ServerSocketChannel) key.channel();
              // Socket akteptieren
              SocketChannel actSocket = actChannel.accept();
              if ( actSocket != null )
                   actSocket.finishConnect();
                   actSocket.configureBlocking( false );
                   actSocket.socket().setTcpNoDelay( true );
                   this.registerSocketList.add( actSocket );
                   this.srvReadSelector.wakeup();
         }Read Data from Socket
        protected int readDatafromSocket( ByteArrayOutputStream socketdata )
             throws IOException
             int readedChars = 0;
            int count = -1;
            Selector readSelector = null;
            SelectionKey tmpKey = null;
            if ( sc.isOpen())
                  ByteBuffer inputbuffer = null;
                 try
                      inputbuffer = bufferpool.getBuffer();
                      while (( count = sc.read( inputbuffer )) > 0 )
                           readedChars += count;
                          inputbuffer.flip();
                           byte[] tmparray=new byte[inputbuffer.remaining()];
                           inputbuffer.get( tmparray );
                           socketdata.write( tmparray );
                          inputbuffer.clear();
                      if ( count < 0 )
                           this.closeSocket();
                           if( readedChars == 0 )
                                readedChars = -1;
                           if ( log.isDebug())
                                  log.debug( "Socket is closed! " );
                      else if ( readedChars == 0 )
                           if ( log.isDebug())
                                  log.debug( "Reread with TmpSelector" );
                           // Glassfish/Grizzly-Implementation
                         readSelector = SelectorFactory.getSelector();
                         if ( readSelector == null )
                              return 0;
                          count = 1;
                          tmpKey = this.sc.register( readSelector, SelectionKey.OP_READ );
                         tmpKey.interestOps(
                              tmpKey.interestOps() | SelectionKey.OP_READ );
                         int code = readSelector.select( 500 );
                         tmpKey.interestOps(
                             tmpKey.interestOps() & ( ~SelectionKey.OP_READ ));
                         if ( code == 0 )
                             return 0;
                             // Return on the main Selector and try again.
                           while (( count = sc.read( inputbuffer )) > 0 )
                                readedChars += count;
                               inputbuffer.flip();
                                byte[] tmparray=new byte[inputbuffer.remaining()];
                                inputbuffer.get( tmparray );
                                socketdata.write( tmparray );
                               inputbuffer.clear();
                           if ( count < 0 )
                                this.closeSocket();
                                if( readedChars == 0 )
                                     readedChars =-1;
                           else if ( count == 0 )
                                  // No data
                 finally
                      if ( inputbuffer != null )
                           bufferpool.releaseBuffer( inputbuffer );
                           inputbuffer = null;
                      // Glassfish-Implementierung
                    if ( tmpKey != null )
                        tmpKey.cancel();
                    if ( readSelector != null)
                        // Bug 6403933
                         try
                            readSelector.selectNow();
                         catch (IOException ex)
                        SelectorFactory.returnSelector( readSelector );
            return readedChars;
        }Thanks for your time.

    I've commented on that blog before. It is rubbish:
    - what does 'overloading the main Selector' actually mean? if anything?
    - 'Although this not clearly stated inside the NIO API documentation': The API documentation doesn't say anything about which Selector you should register channels with. Why would it? Register it with any Selector you like ...
    - 'the cost of maintaining multiple Selectors can reduce scalability instead of improving it' Exactly. So what is the point again?
    - 'wrapping a ByteBuffer inside a ByteBufferInputStream:' Code is rubbish and redundant. java.nio.channels.Channels has methods for this.
    There is no a priori advantage to using multiple Selectors and threads unless you have multiple CPUs. And even then not much, as non-blocking reads and writes don't consume significant amounts of CPU. It's the processing of the data once you've got it that takes the CPU, and that should be done in a separate thread.
    So I would re-evaluate your strategy. I suspect you're getting the channel registered with more than one Selector at a time. Implement it the simple way first then see if you really have a problem with 'overloading the main Selector' ...

  • SocketChannel - problem. Can only read once.

    Hi!
    I'm having some trouble with my accepted SocketChannels from a ServerSocketChannel. The first transmission from the remote host after it has been accept()'ed (I'm using telnet on another mashine to test it) works just fine. But that's it, then I get no more information from the remote host, no matter how much I try.
    I find this very strange, and completely without any reason.
    I've found out that my connection never causes selector.selectNow() to return > 0 after the first transmission, unless there's some other activity but my connection I described above. It doesn't even detect when I close the connection from my telnet-client.
    These three methods are the only one who actually handles ServerSocketChannel and SocketChannel, feel free to help me out =]
    Thanx in advance
       * Thread method, listens for socketrelated events.
      public void run()
        try
          while (true)
            if (selector.selectNow() > 0)
              Set selection = selector.selectedKeys();
              Iterator selectedkeys = selection.iterator();
              while (selectedkeys.hasNext())
                SelectionKey key = (SelectionKey)selectedkeys.next();
                try
                  if (key.isReadable())
                    read(key);
                  else if (key.isConnectable())
                    finishconnect(key);   
                  else if (key.isAcceptable())
                    accept(key);
                catch (CancelledKeyException e)
                  System.out.println("SelectionKey has been cancelled:");
                  e.printStackTrace(System.out);
            else        // If no selection was made, sleep for
            {           // a while to let the processor get some rest =]
              try
                Thread.sleep(50);
              catch (InterruptedException ignore) { }
        catch (ClosedSelectorException e)
          System.out.println("Selector has been closed:");
          e.printStackTrace(System.out);
          System.exit(1);
        catch (Exception e)
          System.out.println("An unforseen error has occurred:");
          e.printStackTrace(System.out);
          System.exit(1);
       * Reads from the socket, and puts it in a ByteBuffer.
       * @param key The selected key from the selector.
      private void read(SelectionKey key)
        try
          SocketChannel socket = (SocketChannel)key.channel();
          Connection conn = (Connection)key.attachment();
          int bytes_read;
          while ((bytes_read = socket.read(buffer)) > 0)
            buffer.flip();
            conn.incoming(buffer);
            buffer.clear();
          if (bytes_read == -1)    // Socket closed.
            try
              key.cancel();
              socket.close();
            catch (IOException ignore) { }
            conn.disconnected("Connection closed by remote host.");
        catch (IOException e)
          System.out.println("I/O exception occurred while reading from socket: " + e);
          e.printStackTrace(System.out);
       * Accepts, and handles incoming sockets from the ServerSocketChannel.
       * @param key The selected key from the selector.
      private void accept(SelectionKey key)
        try
          ServerSocketChannel server = (ServerSocketChannel)key.channel();
          SocketChannel socket = server.accept();
          if (socket != null)
            socket.configureBlocking(false);
            SSConnection ssc = (SSConnection)key.attachment();
            ConnectionListener listener = ssc.getListener();
            int mode = ssc.acceptConnection();
            Connection conn;
            if (mode == ISO_LATIN_CONNECTION)
              conn = new TextConnection(socket, listener, true);
            else
              conn = new Connection(socket, listener, true);
            socket.register(selector, SelectionKey.OP_READ, conn);
            ssc.connectionAccepted(conn);
        catch (IOException e)
          System.out.println("Failed to accept incoming connection:");
          e.printStackTrace(System.out);
      }

    It's opened in the constructor:
       * ConnectionManager - constructor.
       * Opens a selector and starts the thread for this object.
      private ConnectionManager()
        try
          selector = Selector.open();
        catch (IOException e)
          System.out.println("Error occurred while opening selector: " + e);
          System.exit(1);
        new Thread(this).start();
      }

  • Is close() blocking in nonblocking SocketChannel?

    Hi
    I'm writing a socket server based on the NIO package.
    I saw in several NIO code samples that the SocketChannel close() method is invoked inside the NIO select loop, which should only contain nonblocking calls.
    However, I couldn't find any documentation item where it is clearly stated that the SocketChannel close() method is indeed nonblocking (at least when the channel is in nonblocking mode).
    Could someone help me, please?
    Dario

    * Socket channels could conceivably take a
    significant amount of time to close depending on the
    system's networking implementation. Some network
    protocol stacks may block a close while output is
    drained. Your mileage may vary."With all due respect to Hitchens, the behaviour is reasonably well specified for both BSD Sockets and WINSOCK, which are the vast majority of Java sockets platforms. In both cases they only block trying to drain the output if there is output to drain and the linger timeout has been set to non-zero, which is not the default. There may be mainframe or J2ME implementations of TCP which don't comply.

  • SocketChannel.write() blocking application

    Greets,
    I'm developping a huge application and got a latency/block problem using the write(ByteBuffer) method on a Socket from a socketchannel connection.
    Running java 1.5 (diablo) on Freebsd 6 servers, 4Gb ram (2.2 allocated to jvm), with dual xeon dual-core (total 4 cores)
    Here is the application schema :
    - A thread accepting connexion on the socketchannel
    - A thread selecting keys with data to process, enqueuing it after some basic checks on a command FIFO
    - A thread getting commands from the FIFO and processing 'em, generating answers on 4 answer FIFOs
    - 4 threads (1 per FIFO) to get answers and send 'em back to the socket.
    The application usually runs with 4500-5000 simultaneous clients.
    My problem is that the only write() method sometimes takes over 20ms to write a message, with a length smaller than 50 bytes.
    As I got about 25000 answers to process each second, when some of 'em decide to be slow, the 4 threads runs slowly, and all the connected clients are suffering of that latency, for the few minutes needed to empty the FIFOs.
    Every client socket get about 5 answers per second.
    On about 1 hour running, there are about 3 'peaks' of slowness, that I cannot explain yet. That's why I'm in need of advices !
    I monitored the application when such case happens. TOP indicates 40% cpu idle, JVM memory got >500Mb free, network runs @ about 1.2Mbps, where maximal transfer rate is >20Mbps. netstat -m told me no erros, and a large amount of free buffers available.
    As the only slow process is the write() method that usually runs faster than 1ms each call, but in those case I got delays over 20ms.
    freebsd tcp default sendbuffer size is 64k, receive buffer is 32k
    Commands average received size is below 1k, Answers average sending size below 8k.
    This application is running live, and as I cannot emulate 5000+ connections with a similar beahviour to test withour being sure that won't crash all.
    What points could be responsible of such slow write() calls ? Seems it's not CPU, not RAM, not network itself...
    I suppose it's the network buffers that are causing problems. But I don't really know if I have to fit 'em to a lower size, fitting my requirements, or to a larger size, to be sure there won't be full buffers blocking all ?
    I need advices. Thanks for your ideas !
    Bill

    Hmm. So you're happy to lose data?
    A few comments:
    (a) SocketChannels are thread-safe. I don't think you need the synchronization at all, unless maybe multiple writing threads are possible. I would eliminate that possibility and the sync myself.
    (b) If you're getting write delays of 30ms occasionally, the sync must also take 30ms at the same points if it is doing anything at all, i.e. if the possibility of multiple writing threads does exist. So maybe it doesn't?
    (c) I would have a good look at this:
    http://forum.java.sun.com/thread.jspa?threadID=459338
    and specifically the part on how to manage a channel that presents write blocks, using OP_WRITE when it happens and turning it off when it doesn't.
    (d) You seem to be using one output buffer for all channels. You might be better off using a small one per channel. Then that way you don't clear, you just do put/flip/write/compact, and if the write returned 0 just post OP_WRITE for next time around the select loop. Then you won't lose any data at all, except to a client who really isn't reading: you can detect that situation by keeping track of the last successful write time to a channel, and when there is pending data and the last write is too long ago have a think about what the block means in terms of the application. Maybe you should just disconnect the client?
    (e) It would be interesting to know how many times the write loop looped when you get these large delays, and also what the data size was, and also to know that for the other cases to see if there is a difference.
    (f) Generally from a fairness point of view I prefer not to have write loops, just one attempt and if it returns even a short read I post OP_WRITE as above. Otherwise you're spending too long servicing one channel.
    You can contact me offline via http://www.telekinesis.com.au if you like.

  • 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.

  • 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.

  • SocketChannel connection timeouts?

    How can I make a call to the SocketChannel connect method that will timeout after a few seconds rather than minutes in the default implementation? Or is there a particular strategy I could take to get the connection attemp to timeout.

    Perhaps (set up a worker thread to...) sleep() forUnnecessary, use channel.connect(target, timeout) as the other poster recommended. In non-blocking mode, set non-blocking on the channel, then register the channel for OP_CONNECT, call connect(), then enter the select() loop, timing out the connect if it doesn't complete, or testing finishConnect() if OP_CONNECT fires on it and registering OP_READ or OP_WRITE if the connection has succeeded, and off you go ...
    and if it
    is not connected, issue an
    implCloseSelectableChannel()No no no. As documented, close() calls this for you, you never call this yourself.

  • SocketChannel.read(buf) hangs forever

    Hey there :)
    I hope this is the right place to ask a question like this.
    I am writing a client-server app utilizing SocketChannels.
    The client side does this:
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
              buf.clear();
              buf.put("Hello World".getBytes());
              buf.flip();
              int numBytesWritten = sc.write(buf); //11
    and the server does this:
    if (selKey.isValid() && selKey.isReadable()) {               
                   SocketChannel s1Channel = (SocketChannel)selKey.channel();
                   byte[] bytes = new byte[1024];
                   ByteBuffer buf = ByteBuffer.wrap(bytes);
                   buf.clear();
         int numBytesRead = s1Channel.read(buf);
    and at the last line it just hangs forever. I tried changing buffer size and tinkering with everything that came to my mind but nothing helped. Does anyone know what might be the problem here, I'm confused.
    tnx

    Well, the code actually goes like this:
    if (selKey.isValid() && selKey.isReadable()) {
    System.out.println("1");
    SocketChannel s1Channel = (SocketChannel)selKey.channel();
    System.out.println("2");
    byte[] bytes = new byte[1024];
    ByteBuffer buf = ByteBuffer.wrap(bytes);
    buf.clear();
    System.out.println("3");
    int numBytesRead = s1Channel.read(buf);
    System.out.println("4");
    It prints '3' but never gets to '4', so it must hang on that line, right?? I know this makes no sense but that's exactly why I came here to ask....
    Here's a bit more code on the server side that might help:
    //serverSocketChannel is in blocking mode
    serversocketChannel.configureBlocking(true);
              serversocketChannel.socket().bind(new InetSocketAddress(port));
              selector = Selector.open();
    //it is left to the Conector thread to accept connections
    //after very connection accepted it sets the socketChannel to non-blocking mode and registers it with the selector
    //after the first connection it accepts it sets the signal to true
              new Connector(serversocketChannel,selector,startsignal).start();
              while(!startsignal.getSignal()){
                   try {
                        Thread.sleep(1000);
                   } catch (InterruptedException e) {}               
              System.out.println("got it");
              while(true){
                   selector.select();
    in case you need it, here's the run()of the Connector thread:
    while(true){
                   try{
                        System.out.println("Waiting...");
                        SocketChannel sChannel = ssChannel.accept();
                        sChannel.configureBlocking(false);               
                        sChannel.register(selector, sChannel.validOps());
                        startsignal.setSignal(true);
                        Thread.yield();
                   catch(IOException e){
                        ExceptionHandler.handle(e);
              }

  • Possible to trigger select() to continue without new data?

    Hi, I have 2 questions regarding select() and non-blocking sockets.
    1) Is it possible to use select() for multiple client sockets? seems all examples are focusing on the server-side.
    2) Is it possible to tell (by code ofc) the blocking select() method to continue execution even though there are no new events on the attached sockets?
    Thanks.

    So call Selector.select(timeout) with a non-zero timeout so it won't block indefinitely, and when it returns check the selected key set and also process your vector.
    The code for a client socket is exactly the same as the code for an accepted server socket, of which there are plently of examples in the JDK and the Networking Tutorial. The only additional thing is that you have to call connect() on the SocketChannel, register for OP_CONNECT, call finishConnect() when you get it, and deregister OP_CONNECT if finishConnect() returns true.

  • Problem while SocketChannel registers with selector

    Hi ,
    I am coming across problem when i tries to register channel (SocketChannel) with Selector.
    Following is the scenario:
    1) I create selector in main program
    2) I spawns thread and calls (selector.select()) in that thread using created selector in main program
    3) Now I have to do toggle of this created SocketChannel (close() the channel and open() again in main program)
    4) I close the channel in main program ( i have synchronized channel during close operation)
    5) When I try to register again after opening socket my main program hangs forever...Actually
    I am seeing random behavior at this point. Some time it works and registers channel with selector
    and some time it hangs...Socket connection established correctly..just channel for that socket connection
    can not able to register with selector that is currently doing select in another thread...
    Please let me know if anybody come across this kind of problem.
    Thanks a lot,
    Priyank

    Thanks a bunch for the above messages, it actually solved one of my problems, which was,
    I was,
    1. Making serverSocketChannel and registering it with the selector.
    2. Starting selector thread.
    3. Making ClientSocketChannels and registering these with the selector.
    As the selector was started at Step 2, Step 3 was getting blocked, because of selector thread making selector busy, so i read the above messages, and inferred that
    1. Make SerSocketChannels
    2. Make ClientSocketChannels
    3. Start Selector
    and it solved my problem of my program getting blocked at step 3 in the first arrangement. So once again thanks alot to the guys here for making a useful discussion and helping me
    --Aamir                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Multiple selects() within a single VM

    Is anyone aware of any contention issues when running multiple threads within a VM where each thread has it's own select statement?
    I have an home grown app server that allows me to run multiple applications within a single VM. The applications are unrelated however they sometimes communicate with each other via TCP. All of them are very network intensive ( 500 messages a second where each message is around 200 bytes ). These apps are usually single threaded where the main thread is a select() loop.
    Therefore, each application is a single threaded select() loop but there are multiple applications running within a single VM.
    I am seeing performance issues when two apps running within the same VM try to send messages to one another via TCP. When the two apps are running on different boxes one app can send about 10,000 messages a second to the other app. However, when the apps are running within the same VM ( localhost TCP connection ) I can only transfer about 1000 messages a second.
    Are 2 selectors running within the same VM a problem? Is it synchronized within the VM so that only one select can fire at a time?
    I am running on a dual proc RH3 box with plenty of memory.
    Any ideas?

    Works for me, though I'm trying on Windows. Test program below. I get >10,000 replies and responses per second over loopback with both "java SelectPingPong both" (one VM) and running client and server on separate VMs.
    Does this program get only 1000 messages/s on Linux? What VM version?
    import java.util.*;
    import java.net.*;
    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    public class SelectPingPong
        private static final int MESSAGE_SIZE = 200;
        private String server_name;
        private Selector selector;
        private HashMap clients = new HashMap();
        static class Client
         ByteBuffer buf = ByteBuffer.allocate(MESSAGE_SIZE);
         long connect_time = System.currentTimeMillis();
         int number_of_messages;
        public SelectPingPong(int port, String server_name)
         throws IOException
         this.server_name = server_name;
         selector = Selector.open();
         ServerSocketChannel server_channel = ServerSocketChannel.open();
         server_channel.configureBlocking(false);
         server_channel.socket().bind(new InetSocketAddress(port));
         server_channel.register(selector, SelectionKey.OP_ACCEPT);
        public Socket connect(String host, int port)
         throws IOException
         SocketChannel channel = SocketChannel.open();
         Socket socket = channel.socket();
         socket.connect(new InetSocketAddress(host, port));
         configureSocket(socket);
         channel.configureBlocking(false);
         channel.register(selector, SelectionKey.OP_READ);
         clients.put(channel, new Client());
         return socket;
        private void configureSocket(Socket socket)
         throws IOException
         // Let's say we have a request-reply protocol with modest requirements
         socket.setReceiveBufferSize(1024);
         socket.setSendBufferSize(1024);
        public void mainLoop()
         while (true) {
             try {
              selector.select();
              for (Iterator iter = selector.selectedKeys().iterator(); iter.hasNext(); ) {
                  SelectionKey key = (SelectionKey) iter.next();
                  iter.remove();
                  if (!key.isValid())
                   continue;
                  if (key.isAcceptable())
                   acceptClient(key);
                  if (key.isReadable())
                   readFromClient(key);
             } catch (Exception e) {
              System.out.println(server_name + ": error in selector loop: " + e);
              e.printStackTrace();
        private void acceptClient(SelectionKey key)
         throws IOException
         ServerSocketChannel server_channel = (ServerSocketChannel) key.channel();
         if (server_channel == null)
             return;
         SocketChannel channel = server_channel.accept();
         if (channel == null)
             return;
         configureSocket(channel.socket());
         channel.configureBlocking(false);
         channel.register(selector, SelectionKey.OP_READ);
         clients.put(channel, new Client());
         System.out.println(server_name + ": got a new client; " +
                      clients.size() + " clients");
        private void readFromClient(SelectionKey key)
         throws IOException
         SocketChannel channel = (SocketChannel) key.channel();
         Client client = (Client) clients.get(channel);
         ByteBuffer buf = client.buf;
         int count;
         try {
             count = channel.read(buf);
         } catch (IOException e) {
             System.out.println(server_name + ": error reading, closing connection: " + e);
             clients.remove(channel);
             channel.close();
             return;
         if (count == -1) {
             clients.remove(channel);
             System.out.println(server_name + ": client disconnected; " +
                          clients.size() + " clients");
             channel.close();
             return;
         if (buf.position() == MESSAGE_SIZE) {
             if (++client.number_of_messages % 10000 == 0) {
              long now = System.currentTimeMillis();
              System.out.println(server_name + ": " + client.number_of_messages +
                           " messages in " + (now - client.connect_time) + " ms");
             buf.flip();
             // RFE write without blocking
             writeFully(channel, buf);
             buf.rewind();
        public static void writeFully(SocketChannel channel, ByteBuffer buf)
         throws IOException
         while (buf.remaining() != 0)
             channel.write(buf);
        private static void startClient()
         throws Exception
         SelectPingPong client = new SelectPingPong(6667, "client");
         Socket socket = client.connect("localhost", 6666);
         // Send initial message
         ByteBuffer buf = ByteBuffer.allocate(MESSAGE_SIZE);
         buf.put(new byte[MESSAGE_SIZE]);
         buf.flip();
         socket.getChannel().write(buf);
         client.mainLoop();
        public static void main(String args[])
         throws Exception
         if (args.length == 1 && args[0].equals("server")) {
             SelectPingPong server = new SelectPingPong(6666, "server");
             server.mainLoop();
         } else if (args.length == 1 && args[0].equals("client")) {
             startClient();
         } else if (args.length == 1 && args[0].equals("both")) {
             new Thread() { public void run() {
              try {
                  SelectPingPong server = new SelectPingPong(6666, "server");
                  server.mainLoop();
              } catch (Exception e) {
                  System.err.println("error in server");
             } }.start();
             startClient();
         } else {
             System.err.println("usage: SelectPingPong [client | server | both]");
    }

  • 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();
    }

  • SelectionKey.OP_WRITE versus SocketChannel.write(ByteBuffer)

    I'm writing a small Socket server using Non blocking approach based on the ractor design pattern.
    Many samples use the OP_WRITE on the selection keys to know when the channel is ready to accept writes.
    I use the SocketChannel.write(ByteBuffer) whenever I need to write instead.
    Is there a reason why SelectionKey.OP_WRITE is prefered in the samples??????
    Thanks
    Stéphane

    Rainman4500 wrote:
    I'm writing a small Socket server using Non blocking approach based on the ractor design pattern.
    Many samples use the OP_WRITE on the selection keys to know when the channel is ready to accept writes.
    I use the SocketChannel.write(ByteBuffer) whenever I need to write instead.
    Is there a reason why SelectionKey.OP_WRITE is prefered in the samples??????
    So that you don't attempt a write unless the channel can accept some data from you. In your example, suppose you actually transfer 0 bytes because the output buffer is still full. What do you do then?

Maybe you are looking for