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

  • Home
  • SEARCH
  • 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 9150577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:37:47+00:00 2026-06-17T11:37:47+00:00

I just started working with the Boost ASIO library, version 1.52.0. I am using

  • 0

I just started working with the Boost ASIO library, version 1.52.0. I am using TCP/SSL encryption with async sockets. From other questions asked here about ASIO, it seems that ASIO does not support receiving a variable length message and then passing the data for that message to a handler.

I’m guessing that ASIO puts the data into a cyclical buffer and loses all track of each separate message. If I have missed something and ASIO does provide a way to pass individual messages, then please advise as to how.

My question is that assuming I can’t somehow obtain just the bytes associated with an individual message, can I use transfer_exactly in async_read to obtain just the first 4 bytes, which our protocol always places the length of the message. Then call either read or async_read (if read won’t work with async sockets) to read in the rest of the message? Will this work? Any better ways to do it?

  • 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-17T11:37:47+00:00Added an answer on June 17, 2026 at 11:37 am

    Typically I like to take the data I receive in an async_read and put it in a boost::circular_buffer and then let my message parser layer decide when a message is complete and pull the data out.
    http://www.boost.org/doc/libs/1_52_0/libs/circular_buffer/doc/circular_buffer.html

    Partial code snippets below

    boost::circular_buffer TCPSessionThread::m_CircularBuffer(BUFFSIZE);
    
    void TCPSessionThread::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred)
    {
        // ignore aborts - they are a result of our actions like stopping
        if (e == boost::asio::error::operation_aborted)
            return;
        if (e == boost::asio::error::eof)
        {
            m_SerialPort.close();
            m_IoService.stop();
            return;
        }
        // if there is not room in the circular buffer to hold the new data then warn of overflow error
        if (m_CircularBuffer.reserve() < bytes)
        {
            ERROR_OCCURRED("Buffer Overflow");
            m_CircularBuffer.clear();
        }
        // now place the new data in the circular buffer (overwrite old data if needed)
        // note: that if data copying is too expensive you could read directly into
        // the circular buffer with a little extra effor
        m_CircularBuffer.insert(m_CircularBuffer.end(), pData, pData + bytes);
        boost::shared_ptr<MessageParser> pParser = m_pParser.lock(); // lock the weak pointer
        if ((pParser) && (bytes_transferred)) 
            pParser->HandleInboundPacket(m_CircularBuffer); // takes a reference so that the parser can consume data from the circ buf
        // start the next read
        m_Socket.async_read_some(boost::asio::buffer(*m_pBuffer), boost::bind(&TCPSessionThread::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started working using Google Maps API yesterday, and trying to set up
I just recently started working with multithreading with boost::thread , and I'm still a
I just started working on a project using IntelliJ with Play and Scala plugins
I have just started using Boost 1.36. These libraries would be very useful in
I just started working on a aptana based django project from home. The project
I just started working on alfresco v 3.4. I have created document library action
Just started working with .NET and MVC(1). I'm having a problem wherein in my
Just started working with Mercurial a few days ago and there's something I don't
Just started working through the Ruby chapter in Mr. Tate's Seven Language in Seven
I just started working with dependency injection for the first time and I am

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.