4 Replies Latest reply on Dec 21, 2006 12:18 AM by EJP

    java.nio: scatter-gather question

    807607
      Hi,
      i have a question on the following scatter-gather java.nio code. What happens if a message is smaller in size than the (maximum) "messageLength" -if the messages are not of fixed size? The check "(bytesRead < messageLength) " won't work and we will start storing bytes from the next message in the buffer (long r = sc.read( buffers );), where the the previous message had been stored. Is this possible?
      // $Id$
      
      import java.io.*;
      import java.net.*;
      import java.nio.*;
      import java.nio.channels.*;
      
      public class UseScatterGather
      {
        static private final int firstHeaderLength = 2;
        static private final int secondHeaderLength = 4;
        static private final int bodyLength = 6;
      
        static public void main( String args[] ) throws Exception {
          if (args.length!=1) {
            System.err.println( "Usage: java UseScatterGather port" );
            System.exit( 1 );
          }
      
          int port = Integer.parseInt( args[0] );
      
          ServerSocketChannel ssc = ServerSocketChannel.open();
          InetSocketAddress address = new InetSocketAddress( port );
          ssc.socket().bind( address );
      
          int messageLength =
            firstHeaderLength + secondHeaderLength + bodyLength;
      
          ByteBuffer buffers[] = new ByteBuffer[3];
          buffers[0] = ByteBuffer.allocate( firstHeaderLength );
          buffers[1] = ByteBuffer.allocate( secondHeaderLength );
          buffers[2] = ByteBuffer.allocate( bodyLength );
      
          SocketChannel sc = ssc.accept();
      
          while (true) {
      
            // Scatter-read into buffers
            int bytesRead = 0;
            while (bytesRead < messageLength) {
              long r = sc.read( buffers );
              bytesRead += r;
      
              System.out.println( "r "+r );
              for (int i=0; i<buffers.length; ++i) {
                ByteBuffer bb = buffers;
      System.out.println( "b "+i+" "+bb.position()+" "+bb.limit() );
      }
      }

      // Process message here

      // Flip buffers
      for (int i=0; i<buffers.length; ++i) {
      ByteBuffer bb = buffers[i];
      bb.flip();
      }

      // Scatter-write back out
      long bytesWritten = 0;
      while (bytesWritten<messageLength) {
      long r = sc.write( buffers );
      bytesWritten += r;
      }

      // Clear buffers
      for (int i=0; i<buffers.length; ++i) {
      ByteBuffer bb = buffers[i];
      bb.clear();
      }

      System.out.println( bytesRead+" "+bytesWritten+" "+messageLength );
      }
      }
      }
        • 1. Re: java.nio: scatter-gather question
          791266
          Hi,
          i have a question on the following scatter-gather
          java.nio code. What happens if a message is smaller
          in size than the (maximum) "messageLength" -if the
          messages are not of fixed size? The check "(bytesRead
          < messageLength) " won't work and we will start
          storing bytes from the next message in the buffer
          Doesn't that answer your question? You can't do it in that way if you don't know the length of the message.

          Kaj
          • 2. Re: java.nio: scatter-gather question
            807607
            In what way can i work, if i don't know the exact size of a message? Somebody suggested sending the size of the message, before sending the message. This is very complicated to handle and it could turn out to be a destruction if just one byte is lost during the transmission.
            • 3. Re: java.nio: scatter-gather question
              791266
              In what way can i work, if i don't know the exact
              size of a message? Somebody suggested sending the
              size of the message, before sending the message. This
              is very complicated
              No it isn't complicated, and I agree with the one who told you that. That is what you should do.
              to handle and it could turn out
              to be a destruction if just one byte is lost during
              the transmission.
              No bytes will be lost during transmission. TCP handles that.
              • 4. Re: java.nio: scatter-gather question
                EJP
                If you don't know the exact size of a message, or message header, you don't have a reason to use scatter-gather I/O in the first place.