Hi:<br><br>You have a schema in your message. So while encoding and decoding, you have to recognize a complete request or response message. A common practice is to use another delimiter that&#39;s usually not used in protocol message to mark the boundary of different protocol messages. Fortunately, netty do has out-of-box support for this problem. You can have a look at example.telnet package and also codec.frame package to see how DelimiterBasedFrameDecoder can work for you.<br>
<br>Another consideration is that, you have a xml message, of course it is string, but you can build your own encoder and decoder to translate POJO from/to xml string and then xml string from/to ChannelBuffer. That would be better. I think it would be like this:<br>
<br>in your PipelineFactory:<br><br>// this factory should be used for both client and server to setup pipeline<br>public ChannelPipeline getPipeline() throws Exception {<br>        ChannelPipeline pipeline = pipeline();<br>
<br>        final int xmlMessageSizeLimit  = 8192; // can be modified to meet your needs<br>        // Add the text line codec combination first,<br>        pipeline.addLast(&quot;framer&quot;, new DelimiterBasedFrameDecoder(<br>
                xmlMessageSizeLimit, Delimiters.nulDelimiter()));<br>        pipeline.addLast(&quot;decoder&quot;, new MyProtocolDecoder());<br>        pipeline.addLast(&quot;encoder&quot;, new MyProtocolEncoder());<br><br>
        // and then business logic.<br>        pipeline.addLast(&quot;handler&quot;, handler);<br>}<br><br>and then define your own encoder and decoder:<br><br>public class MyProtocolDecoder extends StringDecoder{<br>      //just override decode method<br>
      protected Object decode(<br>            ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {<br>        String xml = super(ctx, channel, msg);<br>        // then use any xml parser or xml binding tool to map xml to your POJO, I use XStream as example<br>
        XStream mapper = new XStream();<br>        mapper.alias(&quot;header&quot;, MyProtocolMessage.MyProtocolHeader.class);<br>        mapper.alias(&quot;body&quot;, MyProtocolMessage.MyProtocolBody.class);<br>        MyProtocolMessage protoMsg = mapper.fromXML(xml);<br>
        return protoMsg;<br>    }     <br>}<br><br>public class MyProtocolMessage {<br>   MyProtocolHeader header;<br>   MyProtocolBody body;<br>   <br>   static class MyProtocolHeader {......}<br>   static class MyProtocolBody {......}<br>
   <br>}<br><br>you should do the reverse process in MyProtocolEncoder<br><br>Hope it helps.<br><br>good luck<br><br>yanky<br><br><br><div class="gmail_quote">2009/4/13 manishr22 <span dir="ltr">&lt;<a href="mailto:manishr22@gmail.com">manishr22@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Hi,<br>
<br>
How I can read a chunk of data from channel using netty api? my client is writing every time on socket<br>
<div class="im"><br>
&lt;manish&gt; welcome to the real world&lt;/manish&gt;<br>
<br>
</div>and my netty server needs to read the message till &lt;/manish&gt; in one go. and then next message starting from &lt;manish&gt; should be read by Netty server.<br>
<br>
<br>
Any code snippet would be a great help.<br>
<br>
In my decoder if i read the message from channelbuffer using getbytes() api i see the data but not able to get i can read the whole message in one go. i do see sometime some extra message being read from channel. any reason why it should be.<br>

<div><div></div><div class="h5"><br>
<br>
<br>
Thanks in advance.<br>
Regards<br>
Manish<br>
<br>
Hi Yanky,<br>
<br>
Thanks so much for your guidance. I will look into factorial example will solving my problem based on this.<br>
<br>
For any further help, if needed i will post my request here.<br>
<br>
Thanks again<br>
<br>
Manish<br>
<br>
<br>
<br>
Thanks Yanky for the great answer!  So nice that you are here as a<br>
part of the community.<br>
<br>
Cheers,<br>
<br>
— Trustin Lee, <a href="http://gleamynode.net/" target="_blank">http://gleamynode.net/</a><br>
<br>
On Sat, Apr 11, 2009 at 3:16 AM, yanky young &lt;<a href="mailto:yanky.young@gmail.com">yanky.young@gmail.com</a>&gt; wrote:<br>
&gt; Hi:<br>
&gt;<br>
&gt; I am not netty expert yet. I am just trying to give some hints. In your<br>
&gt; case, you are actually building a request/response protocol. And request and<br>
&gt; response message may include quite complicated headers and body. So you have<br>
&gt; to do some message encoding and decoding in both server side and client<br>
&gt; side. You can see in netty codebase there are some out-of-box encoder and<br>
&gt; decoder in handler.codec package. I suggest you take a look at<br>
&gt; example.fractorial package to see how to implement your own encoder and<br>
&gt; decoder for your message format which may be xml. If you are not sensitive<br>
&gt; to message format, you can also have a look at example.localtime which use<br>
&gt; google protobuf library as real encoder and decoder.<br>
&gt;<br>
&gt; good luck<br>
&gt;<br>
&gt; yanky<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; 2009/4/11 manishr22 &lt;<a href="mailto:manishr22@gmail.com">manishr22@gmail.com</a>&gt;<br>
&gt;&gt;<br>
&gt;&gt; Hi Yanky,<br>
&gt;&gt;<br>
&gt;&gt; I took the sample code from svn trunk and saw the codes too. Could you<br>
&gt;&gt; please let me know which one among given sample code will fit for the<br>
&gt;&gt; reference of my requirement.<br>
&gt;&gt;<br>
&gt;&gt; Regards,<br>
&gt;&gt; Manish<br>
&gt;&gt;<br>
&gt;&gt; Hi:<br>
&gt;&gt;<br>
&gt;&gt; Of course there are some sample code in netty project. You can just check<br>
&gt;&gt; out the svn trunk and see some samples in example package.<br>
&gt;&gt;<br>
&gt;&gt; good luck<br>
&gt;&gt;<br>
&gt;&gt; yanky<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; 2009/4/10 manishr22 &lt;<a href="mailto:manishr22@gmail.com">manishr22@gmail.com</a>&gt;<br>
&gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Hi,<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I had a problem statement<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; &quot;to write an asynchronous TCP server to which multiple clients will<br>
&gt;&gt; &gt; connect<br>
&gt;&gt; &gt; and keep sending messages to my tcp server. My server should read the<br>
&gt;&gt; &gt; message and send back some acknowledgment information to the client who<br>
&gt;&gt; &gt; sent<br>
&gt;&gt; &gt; the data. While reading the data at server since the message comes in<br>
&gt;&gt; &gt; particular format i have to do some processing before sending the<br>
&gt;&gt; &gt; response<br>
&gt;&gt; &gt; back to client.&quot;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; say my client is sending a message structure in which it has some header<br>
&gt;&gt; &gt; and then in body it has message like :<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; &lt;manish&gt;welcome to the real world&lt;/manish&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I was on the way to implement NIO but in between i found such a great<br>
&gt;&gt; &gt; library netty and thought of using it. i was trying to make use of this<br>
&gt;&gt; &gt; library with the given tutorial and examples but i found that tutorial<br>
&gt;&gt; &gt; is<br>
&gt;&gt; &gt; not good enough to solve the complex problem.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; If you can provide me some guidance or sample code that how i should<br>
&gt;&gt; &gt; solve<br>
&gt;&gt; &gt; my problem using netty library, that would be great.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Thanks in advance.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Regards<br>
&gt;&gt; &gt; Manish<br>
&gt;&gt; &gt; --<br>
&gt;&gt; &gt; View this message in context:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; <a href="http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2615889.html" target="_blank">http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2615889.html</a><br>
&gt;&gt; &gt; Sent from the Netty User Group mailing list archive at Nabble.com.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; netty-users mailing list<br>
&gt;&gt; &gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; netty-users mailing list<br>
&gt;&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; View this message in context:<br>
&gt;&gt; <a href="http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2617267.html" target="_blank">http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2617267.html</a><br>
&gt;&gt; Sent from the Netty User Group mailing list archive at Nabble.com.<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; netty-users mailing list<br>
&gt;&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; netty-users mailing list<br>
&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;<br>
&gt;<br>
<br>
_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
<br>
<br>
<br>
<br>
<br>
--<br>
</div></div>View this message in context: <a href="http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2627351.html" target="_blank">http://n2.nabble.com/Code-example-to-write-asynchronous-server-tp2615889p2627351.html</a><br>

<div><div></div><div class="h5">Sent from the Netty User Group mailing list archive at Nabble.com.<br>
<br>
<br>
_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
</div></div></blockquote></div><br>