Example: multiple ChannelHandlers
For example, you could split TimeClientHandler into two handlers: TimeDecoder which deals with the fragmentation issue, and the initial simple version of TimeClientHandler. Fortunately, Netty provides an extensible class which helps you write the first one out of the box: package org.jboss.netty.example.time; public class TimeDecoder extends FrameDecoder { @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) { if (buffer.readableBytes() < 4) { return null; } return buffer.readBytes(4); } } FrameDecoder is an implementation of ChannelHandler which makes it easy to which deals with the fragmentation issue. FrameDecoder calls decode method with an internally maintained cumulative buffer whenever new data is received. If null is returned, it means there's not enough data yet. FrameDecoder will call again when there is a sufficient amount of data. If non-null is returned, it means the decode method has decoded a message successfully. FrameDecoder will discard the read part of its internal cumulative buffer. Please remember that you don't need to decode multiple messages. FrameDecoder will keep calling the decoder method until it returns null. Now that we have another handler to insert into the ChannelPipeline, we should modify the ChannelPipelineFactory implementation in the TimeClient: bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { return Channels.pipeline( new TimeDecoder(), new TimeClientHandler()); } }); If you are an adventurous person, you might want to try the ReplayingDecoder which simplifies the decoder even more. You will need to consult the API reference for more information though. package org.jboss.netty.example.time; public class TimeDecoder extends ReplayingDecoder { @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, VoidEnum state) { return buffer.readBytes(4); } } Additionally, Netty provides out-of-the-box decoders which enables you to implement most protocols very easily and helps you avoid from ending up with a monolithic unmaintainable handler implementation. Please refer to the following packages for more detailed examples: org.jboss.netty.example.factorial for a binary protocol, and org.jboss.netty.example.telnet for a text line-based protocol.