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 8108665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:11:09+00:00 2026-06-06T01:11:09+00:00

i have a c++ server which is using boost::asio to do read/write operations –

  • 0

i have a c++ server which is using boost::asio to do read/write operations – writing out the messages works fine – yet for some reason i cannot get the read to work

the message i am sending to it from the client is 15 16 bit unsigned shorts – my test message is this:

1, 34, 7, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0

now on the server i’m seeing things like this constantly. the read is often broken up and/or multiplied by 256

this is one run sending it twice

reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 3: [1024 0 0]
reading length = 3: [0 0 0]
reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 6: [1024 0 0 0 0 0]

this is the second run sending it twice

reading length = 8: [1 34 7 0 0 0 0 0]
reading length = 6: [1024 0 0 0 0 0]
reading length = 6: [1 34 7 0 0 0]
reading length = 1: [0] 
reading length = 0: []
reading length = 0: []
reading length = 2: [0 0]
reading length = 2: [0 0]
reading length = 1: [0]

this is the third run sending it once (i don’t know what happened here)

reading length = 14: [256 8704 1792 0 0 0 0 0 1024 0 0 0 0 0]

Here is the actual server read code

void Session::start(void) {
  msg_interface->add_msg_listener(this);
  _socket.async_read_some(
    boost::asio::buffer(_read_data_buffer, READ_DATA_BUFFER_LENGTH),
    boost::bind(&Session::handle_read, this, boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void Session::handle_read(const boost::system::error_code& error, std::size_t bytes_transferred) {
  if (error) {
    msg_interface->remove_msg_listener(this);
    delete this;
  } else {
    if (bytes_transferred != 0) {
      // print what we've received
      const int length = bytes_transferred / sizeof(uint16_t);
      std::cout << "reading length = " << length << ": [";
      for (int i = 0; i < length; i++) {
        const uint16_t data = ntohs(_read_data_buffer[i]);
        std::cout << data;
        if (i != length - 1) {
          std::cout << " ";
        }
      }
      std::cout << "]" << std::endl;
    }
    _socket.async_read_some(
      boost::asio::buffer(_read_data_buffer, READ_DATA_BUFFER_LENGTH),
      boost::bind(&Session::handle_read, this, boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
  }
}

the variables are as follows

const int READ_DATA_BUFFER_LENGTH = 1024;

class AbstractSession {
protected:
  uint16_t _read_data_buffer[READ_DATA_BUFFER_LENGTH];
...

There is one thing – i am using the same IO service to write messages when they are received (the server is talking to an old interface and sends out the messages it receives to all the clients) and to read messages from the clients. could using the same io service for both cause this to happen?

Any thoughts on what i should do to fix this?

  • 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-06T01:11:10+00:00Added an answer on June 6, 2026 at 1:11 am

    Your code does what it is supposed to do – it reads as much data as it can. This is why the function async_read_some name ends with “some” – it reads “some” bytes and does not promise to do more than that. Documentation also says the following:

    Consider using the async_read function if you need to ensure that the
    requested amount of data is read before the asynchronous operation
    completes.

    Which seems like your case unless you re-work your receiver to handle partial reads.

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

Sidebar

Related Questions

I'm trying to write a SSL-based async server using Boost ASIO example code from
I am writing an application using Boost asio in which the client and the
I have a java server which is using TCP and sockets to connect to
I have a Windows 2008 Server with IIS 7 which using a .NET C#
Using ASP.Net, I have a server control for which i would like to add
I have a web application, which is using a SQL Server 2005 database. My
I have a program app1 which launches another application using: app2Server := CreateOleObject('app2.Server'); On
I am using Microsoft SQL Server. I have a Table which had been updated
I'm using Sql-Server, and I have the below query which returns all columns that
I have a web server which is protected behind http-basic-auth. I've read through the

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.