Hi,

I have problems with garbled data send by a StreamIOHandler. Here is my 
code (short Version ;-):

Server:
=====

new SocketAcceptor().bind(
    new InetSocketAddress(port),
    new StreamIoHandler() {
        protected void processStreamIo(
            IoSession    session,
            InputStream  in,
            OutputStream out
        ) {
           new Thread(new Worker(in, out)).start();
       }
    },
    new SocketAcceptorConfig()
);


public class Worker implements Runnable {
  private InputStream is;
  private OutputStream os;

  public Worker(InputStream is, OutputStream os) {
    this.is = is;
    this.os = os;
  }

  public void run() {
    // read request
    ...

    // write response

    byte[] buffer = new byte[1024];
      for (int i = 0; i < 100; i++) {
        for (int j = 0; j < buffer.length; j++) {
        buffer[j] = (byte) (i + j);
        }
        // buffer.clone() because of Bug DIRMINA-369
        os.write((byte[]) buffer.clone());
        // os.flush() to wait for completion (necessary?)
        os.flush();
      }
   }
}


Client
=====

Socket socket = new Socket(adress, port);

// write request

socket.getOutputStream(). ...;

// read response

InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
int size = 0;
int blockCount = 0;
while ((size = is.read(buffer)) > 0) {
  for (int i = 0; i < buffer.length; i++) {
    if ((byte) (blockCount + i) != buffer[i]) {
      throw new RuntimeException("garbled data");
    }
  }
  blockCount++;
}


---------------------------

If I choose the buffer for send and receive small enough (e. g. 64 byte) 
everthing works. But with a buffersize of 1024 bytes the response data is 
garbled. If I insert a sleep of 50 ms at serverside between every write, 
it works again, even with a buffersize of 1024 bytes.

Thanks for help
Markus

Reply via email to