4 Replies Latest reply on Aug 3, 2010 5:02 AM by 843790

    can't write full message on socketChannel write method


      i am using NIO to handle connections and i am having problems trying to write a full message to the socket, but it does not happen always,

      now, below is a piece of code of my write method,

      when it writes i realized it keeps saying the method write returns 0 but still the byteBuffer has data pending to be written,
       bs = 8
       public boolean write(String msg)
                      _bb = ByteBuffer.allocate(bs + msg.length());
                  int bw = _channel.write(_bb);
                  while (_bb.hasRemaining()) {
                      if (_channel.write(_bb) == 0) {
                  if(_bb.remaining() != 0)
                       //log the message it was not sent the full message
                  _bb = null;
              }//exception handler
      i had that method before without the while loop and still with the loop, it does not write the full message, and as I said, this does not happen often.

      this server acts as load balancer, and the message it is trying to write is the message received from other java server i have in other physical server, i have 3 physical servers 2 for games and the other as load balancer,

      this load balancer when it receives a connections registers the channel and wrap it into an object which is the attachment, and into the object it connects to the next available server whit better performance,

      this load balancer has 2 selector keys, 1 for incoming connections and the other that connects to another available server and both selectors works independently.

      if there is something someone can help me with, i really appreciate it.

        • 1. Re: can't write full message on socketChannel write method
          Write returns zero when your socket's send buffer fills up. That in turn happens when the receiver's socket receive buffer fills up, and that in turn happens when the read is slow reading. Not much you can do about that except wait. That's what OP_WRITE is for. When you get the zero, register OP_WRITE instead of OP_READ and return to the select loop; when you get the OP_WRITE event, try the write again; if it succeeds, switch back to OP_READ.
          • 2. Re: can't write full message on socketChannel write method
            can you provide me a sample code for this situation.

            because i read from one channel when the read option is fired, and i get the data, and i write that in other channel that connects with the client outside.
            so, i have a handler and another handler as attachement one of the other, it is like a cross action.

            • 3. Re: can't write full message on socketChannel write method
              I've practically provided you with pseudo-code. That's usually as far as I go for nothing. Writing the code is your job. The Internet is full of examples. Try for instance the very long thread here called 'Taming the NIO Circus', although you can ignore most of it except the contributions by pkwooster and myself.
              • 4. Re: can't write full message on socketChannel write method
                thank you ejp, after reading the full article, i could get my app working without loosing packages

                what i did was,

                i added a linked list to have a queue of messages , and when the loop checks that i have a signal for writing, then i check if the buffer has remaining bytes to be written,
                if it does i write the bytes, if still are remaining bytes then i register the signal for writing again for the next loop,

                in the other hand i take the first message from the linkedlist and i write the bytes and the thing loops in the same thing.

                thank you very much.