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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:04:46+00:00 2026-05-27T13:04:46+00:00

I have been stuck with this prolem for the past 5 hours or so..

  • 0

I have been stuck with this prolem for the past 5 hours or so.. Sorry if the question is too obvious/noob but I am noob myself when it comes to boost::asio or tcp/ip in general.

So here is the problem. I am trying to modify the blocking tcp server example :

void session(socket_ptr sock)
{
  try
  {
    for (;;)
    {
      char data[max_length];

      boost::system::error_code error;
      size_t length = sock->read_some(boost::asio::buffer(data), error);
      if (error == boost::asio::error::eof)
        break; // Connection closed cleanly by peer.
      else if (error)
        throw boost::system::system_error(error); // Some other error.

      boost::asio::write(*sock, boost::asio::buffer(data, length));
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception in thread: " << e.what() << "\n";
  }
}

What I want to do is save all the chunks or the read_some method into some buffer for this example std::string and then do something with them before sending a reply back:

const int max_length = 10;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock)
{
    std::string dataReceived = "";
    try
    {
        for (;;)
        {
            char data[max_length] = {'\0'};
            boost::system::error_code error;
            size_t length = sock->read_some(boost::asio::buffer(data), error);
            dataReceived += std::string(data, length);
            if (error == boost::asio::error::eof)
                break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.
            //boost::asio::write(*sock, boost::asio::buffer(&dataReceived[0], dataReceived.size()));
        }
        boost::asio::write(*sock, boost::asio::buffer(&dataReceived[0], dataReceived.size()));
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

When I remove the write from inside the loop the client hangs. The dataReceived has all the data inside. The buffer is deliberately small so that read_some is called more than once. In the debugger the control never goes to the write method outside of the loop. I am probably doing something very wrong. But I am unable to find out what.

Side question:

What would be the simplest solution to have a socket connection between some UI and a backend process?

  • 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-05-27T13:04:47+00:00Added an answer on May 27, 2026 at 1:04 pm

    Probably it hangs because client waits for server reply and don’t send new data.

    Also, server will exit form loop only when connection is closed, and there is nowhere to write data.

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

Sidebar

Related Questions

Sorry for the long question, but I have been stuck on this for days
I understand this is a basic question... but I have been stuck on it
I have been stuck on this problem for many hours, but I have a
I have been stuck for a long time on this simple but obtuse message
i know this is basic but somehow i have been stuck here for some
I've been working on this for the past several days and have been stuck.
I have spent the past three days researching this... but Im still stuck. I
I have been trying to find a solution for past few hours but unable
Hope to get solution to this problem. I have been stuck on it since
I have been stuck on this for a while now and I cannot seem

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.