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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:31+00:00 2026-05-23T10:40:31+00:00

I have a server that’s currently using asynchronous IO to (1) accept data from

  • 0

I have a server that’s currently using asynchronous IO to (1) accept data from cin and send that input across a UNIX socket and (2) accept data on a UNIX socket, parse that data, and send back a response.

When I’m parsing the data, how can I block the I/O that occurs in the parser (ParseJSON(data_.data()) in the example below)? It should ask questions and collect answers to those questions via cin before returning parsed for the async_write; currently, the questions are printed, but the response is sent before the answers are input to cin.

socket_ is a stream_protocol::socket from boost::asio, FWIW.

void handle_read(const boost::system::error_code& error,
                 size_t bytes_transferred)
{
    if (!error)
    {
        string parsed = ParseJSON(data_.data());
        async_write(socket_,
            buffer(parsed),
            boost::bind(&session::handle_write,
                shared_from_this(),
                placeholders::error));
    }
}

void handle_write(const boost::system::error_code& error)
{
    if (!error)
    {
        socket_.async_read_some(buffer(data_),
            boost::bind(&session::handle_read,
                shared_from_this(),
                placeholders::error,
                placeholders::bytes_transferred));
    }
}

An abbreviated version of what happens in ParseJSON (Some strings replaced with <> for confidentiality):

string ParseJSON(string input)
{
    int status;
    string toWrite;

    JSONNode parsed = libjson::parse(input);
    JSONNode::const_iterator iter = parsed.begin(); 
    json_string node_name = iter -> as_string();

    if (node_name.compare("<>") == 0)   
    {
        cout << "<>" << endl;
        cout << "<>";
        cout.flush();
        cin >> status;

        JSONNode n(JSON_NODE);
        n.push_back(JSONNode("<>","<>"));
        JSONNode c(JSON_NODE);
        c.set_name("args");
        c.push_back(JSONNode("<>",status));
        n.push_back(c);
        toWrite = n.write();
        return toWrite;
    }
}
  • 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-23T10:40:32+00:00Added an answer on May 23, 2026 at 10:40 am

    If ParseJSON() does a blocking read from cin then it will block which means that handle_read() won’t execute the async_write() until ParseJSON() returns.

    I think the problem is that parsed is a stack variable. handle_read() will most likely return before the data is actually written and parsed will be destroyed. The data passed to async_write() needs to be valid until the completion handler (handle_write) is called.

    If handle_read() and and handle_write() are member functions you could add a parsed member to hold the data. Alternatively, you could wrap parsed in shared_ptr that is bound to handle_write() something like this:

    void handle_read(const boost::system::error_code& error,
                     size_t bytes_transferred)
    {
        if (!error)
        {
            string *ps = new string(ParseJSON(data_.data()));
            boost::shared_ptr<string> pParsed(ps);
            async_write(socket_,
                buffer(*pParsed),
                boost::bind(&session::handle_write,
                    shared_from_this(),
                    pParsed,
                    placeholders::error));
        }
    }
    
    void handle_write(boost::shared_ptr<string> pParsed, 
                      const boost::system::error_code& error)
    {
        if (!error)
        {
            socket_.async_read_some(buffer(data_),
                boost::bind(&session::handle_read,
                    shared_from_this(),
                    placeholders::error,
                    placeholders::bytes_transferred));
        }
    }
    

    When the completion handler exits, the last reference to the shared_ptr will disappear and ps will be destroyed.

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

Sidebar

Related Questions

I have a server that sends data via a socket, the data is a
I have a server that hosts my Subversion code base. That server is currently
I have a server that connects to multiple clients using TCP/IP connections, using C
We code in C# using VS2008 SP1. We have a server that runs Team
I have a server that receives a compressed string (compressed with zlib) from a
I have a server that should send a broadcast transimission, which I have to
Say I want to have a server that can accept 2GB file over network,
I have server that needs to process and dump from an SQL database queries
I have a server that streams data out on a TCP port once you
I have a server that accesses an external API not from our company via

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.