Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well you can always decide how many bytes to write at a time, but you definitely never know when and how many bytes are received (this is why NIO makes sense). You need to handle you own buffer for receiving a fix number of bytes you want. To do so you can use a <strong>FrameDecoder</strong> which is design for this purpose.</p> <p>Additionaly, you can make sure the datas does not stay too long in the sender socket buffer by setting <strong>tcpNoDelay</strong> to true, so it will no wait for the current "frame" to reach a certain critical size before physically sending the datas.</p> <p>If I understand well, you are writing let's say 2048 Bytes in one hand but all the datas are not received in the messagedReceived event on the other hand? Try to check these common issues:</p> <ul> <li>you application terminates too early and the datas are not yet arrived</li> <li>your datas are stucked in the socket buffer of the "sender" because you did not <strong>close</strong> the Channel and <strong>tcpNoDelay</strong> option was not set to true. This is causing the socket to wait for some additional bytes before sending the packet.</li> <li>you did not read all the datas inside the ChannelBuffer but for a reason the <strong>readerIndex</strong> as been set to a further position</li> </ul> <p>Try to show us some part of your code, it should make things easier...</p> <p>ADDED 17/04/2012</p> <p>If I understand you are trying to pass a byte array coding for a String from the sender to the receiver. Here is your code after a littel refactor:</p> <p>----------------------------code ----------------------------write hand: response.size()>1024bytes</p> <pre><code>byte[] datas = ((String)msg).getBytes("UTF-8"); //ALWAYS SPECIFY THE ENCODING ChannelBuffer buffer = ChannelBuffers.wrap(datas); //USE DIRECTLY THE ARRAY System.out.println(buffer); //buffer'size&gt;1024 here channel.write(buffer); </code></pre> <p>----------------------------recieve hand: should recieve twice,println() would execute twice</p> <pre><code>ChannelBuffer buffer = (ChannelBuffer) event.getMessage(); System.out.println(buffer) //buffer'size once 1024,once the remainder size byte[] datas =buffer.readBytes(buffer.readableBytes()).array() String msg=new String(datas , "UTF-8"); //BAD IDEA because the bytes sequence of the last UTF-8 char could be uncompleted there System.out.println(str); </code></pre> <p>This is not the way to do that, you should instead use directly the <strong>StringEncoder</strong> and <strong>StringDecoder</strong> in the package <strong>org.jboss.netty.handler.codec.string</strong>. It will handle the Framing problem for you. If you still want to debug your code, use the <strong>LoggingHandler</strong> provided by Netty. Also did you really set this option:</p> <pre><code>bootstrap.setOption("tcpNoDelay", true); </code></pre> <p>in both sides bootstraps?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload