You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Ma...@tk-online.de on 2007/07/18 13:31:24 UTC

Garbled Data with StreamIoHandler

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

Antwort: Re: Garbled Data with StreamIoHandler

Posted by Ma...@tk-online.de.
Hi Trustin,

thank you for the quick response, but unfortunatley the new MINA-Release 
1.1.1 doesn't solve my problem. The problem described below still occurs.

Here are some infos of my environment:

Windows 2000 SP 4
JDK 1.5.0_06 + JDK 1.6.0_01
MINA 1.1.1


Markus


"Trustin Lee" <tr...@gmail.com> schrieb am 18.07.2007 16:18:20:

> Hi Markus,
> 
> Which version of MINA are you using?  We've just released a new
> version that fixes the problem you are experiencing:
> 
> http://www.apache.org/dyn/closer.cgi/mina/1.1.1/
> 
> We didn't announce the new release because of propagation delay
> between mirrors.  Please use the backup sites if you get 404.
> 
> HTH,
> Trustin
> 
> On 7/18/07, Markus.Hampel@tk-online.de <Ma...@tk-online.de> 
wrote:
> > 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
> 
> 
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6

Re: Garbled Data with StreamIoHandler

Posted by Trustin Lee <tr...@gmail.com>.
Hi Markus,

Which version of MINA are you using?  We've just released a new
version that fixes the problem you are experiencing:

http://www.apache.org/dyn/closer.cgi/mina/1.1.1/

We didn't announce the new release because of propagation delay
between mirrors.  Please use the backup sites if you get 404.

HTH,
Trustin

On 7/18/07, Markus.Hampel@tk-online.de <Ma...@tk-online.de> wrote:
> 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


-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Re: Garbled Data with StreamIoHandler

Posted by Trustin Lee <tr...@gmail.com>.
Hi Markus,

On 7/18/07, Markus.Hampel@tk-online.de <Ma...@tk-online.de> wrote:
<snip/>

> Client
> =====

<snip/>

> while ((size = is.read(buffer)) > 0) {
>   for (int i = 0; i < buffer.length; i++) {

Shouldn't this be:

for (int i = 0; i < size; i++) {

?  InputStream.read() doesn't already fill the buffer to its end.  You
could wrap the InputStream with DataInputStream and use readFully
instead.  Please let me know if this works.

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6