Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:33:31+00:00 2026-06-17T12:33:31+00:00

I am currently evaluating Netty to handle socket comms for a Java client to

  • 0

I am currently evaluating Netty to handle socket comms for a Java client to integrate with a C++ server. The messaging protocol has the following structure –

  • (Type)(SubType)(Length)(MessageBody) with size
  • <4bytes><4bytes><4bytes><…> – The Length includes the header.

Following the Netty api I subclass LengthFieldBasedFrameDecoder to receive a valid full packet and then decode each packet depending on the type received. From docs I’m using –

  • lengthFieldOffset = 8
  • lengthFieldLength = 4
  • lengthAdjustment = -12 (= the length of HDR1 + LEN, negative)
  • initialBytesToStrip = 0

It works fine for about 5 minutes (I’m getting one message every 5 seconds or so) and then the decode event contains a ChannelBuffer that is much shorter than the size of the message. (I have received this message multiple times before the crash). I then obviously get an BufferUnderflowException in my internal decode code. Am I doing something wrong? Should I be guaranteed the correct sized buffer for the message when using LengthFieldBasedFrameDecoder?

LengthFieldBasedFrameDecoder class –

public class CisPacketDecoder extends LengthFieldBasedFrameDecoder
{
    public CisPacketDecoder(int maxFrameLength, int lengthFieldOffset,
            int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment,
                initialBytesToStrip);       
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf)
            throws Exception 
    {       
        CisMessage message = null;      
        int type = buf.getInt(0); //Type is always first int
        CisMessageType messageType = CisMessageType.fromIntToType(type);
        if(messageType != null)
        {
            message = messageType.getObject();
            if(message != null)
            {
                message.decode(buf.toByteBuffer());
            }
            else
            {
                System.out.println("Unable to create message for type " + type);
            }
        }

        //mark the Channel buf as read by moving reader index
        buf.readerIndex(buf.capacity());
        return message;
    }

}

And instantiated here.

public class PmcPipelineFactory implements ChannelPipelineFactory 
{

    @Override
    public ChannelPipeline getPipeline() throws Exception
    {
        ChannelPipeline pipeline = Channels.pipeline();

        pipeline.addLast("encoder", new CisPacketEncoder());
        pipeline.addLast("decoder", new CisPacketDecoder(1024, 8, 4, -12, 0));      
        pipeline.addLast("handler", new MsgClientHandler());
        return pipeline;
    }


} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:33:31+00:00Added an answer on June 17, 2026 at 12:33 pm

    You need to call super.decode(..) and operate on the returned ChannelBuffer in your decode(..) method.

    So it would be like this;

    public class CisPacketDecoder extends LengthFieldBasedFrameDecoder {
        public CisPacketDecoder(int maxFrameLength, int lengthFieldOffset,
                int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
            super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment,
                initialBytesToStrip);       
        }
    
        @Override
        protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf)
            throws Exception {
    
            // THIS IS IMPORTANT!!!!!
            ChannelBuffer decoded = (ChannelBuffer) super.decode(ctx, channel, buf);
            if (decoded == null) {
                return null;
            }
    
            // NOW ONLY OPERATE ON decoded 
            CisMessage message = null;      
            int type = decoded.getInt(0); //Type is always first int
            CisMessageType messageType = CisMessageType.fromIntToType(type);
            if(messageType != null) {
                message = messageType.getObject();
                if(message != null) {
                    message.decode(decoded.toByteBuffer());
                } else {
                    System.out.println("Unable to create message for type " + type);
                }
            }
            return message;
        }
    }
    

    Be sure to check the UPPERCASE comments .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning on introducing Java rules and currently in the process of evaluating
I have currently evaluating Java based security frameworks, I am a Spring 3.0 user
We are currently evaluating the mobile test automation tools for our reputed client. So
I'm currently evaluating the .net client profile for a future project, and there are
I'm currently evaluating Windows Server AppFabric as a distributed cache solution. If we implement
I am currently evaluating Protocol Buffers for use in a project (no code written
We're currently evaluating the WSO2 Identity Server and I got a few questions about
Currently I am evaluating number of web service frameworks in Java. I need web
I'm evaluating Microsoft Team Foundation Server for my customer, who currently uses Visual SourceSafe
I'm currently evaluating the right solution for the following app: The main goal is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.