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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:05:30+00:00 2026-06-11T15:05:30+00:00

i have a problems with ASIO boost library. The problem are asynchronous functions. This

  • 0

i have a problems with ASIO boost library. The problem are asynchronous functions.

This is simple server code.

void handle_accept( const boost::system::error_code& error )
{
    std::cout << "Someone connected" << std::endl;
}

void handle_read( const boost::system::error_code& error )
{
    printf( "Message: %s \n", somedata);
}

int main()
{

std::cout << "Starting server ....\n" << std::endl;

try
{
    boost::asio::io_service io_service;
    tcp::socket  mysocket(io_service);
    tcp::acceptor myacceptor(io_service, tcp::endpoint(tcp::v4(), 5000));

    myacceptor.async_accept( mysocket, boost::bind( &handle_accept, boost::asio::placeholders::error));
    mysocket.async_receive(  boost::asio::buffer(somedata, 1024)  , boost::bind( &handle_read, boost::asio::placeholders::error) );
    io_service.run();

    std::cout << "END. \n" << std::endl;
    sleep(5);

}  catch (std::exception& e)
{
  std::cerr << "Exception: " << e.what() << "\n";
}
return 0;

}

and client code here

int main()
{
std::cout << "Starting client ....\n" << std::endl;

try
{
boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), "localhost", "5000");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket mysocket(io_service);

boost::asio::connect(mysocket, endpoint_iterator);

sleep(5);
sprintf( somedata, "This is a message i sent.");
mysocket.send( boost::asio::buffer(somedata, 1024)  );

}  catch (std::exception& e)
{
  std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}

So, How it should work. Server should wait on connection from client, when client is connected, it should call handle_accept. When client is connected, handle_accept is really called, but it also calls handle_read even no data is recieved !!! why ? client sends data after 5 seconds after making connection.

thank you very much.

  • 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-11T15:05:31+00:00Added an answer on June 11, 2026 at 3:05 pm

    The receive completes immediately because it cannot wait because it has nothing to wait for. No data will ever be received on a TCP listening socket. Call async_receive on your connected sockets only.

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

Sidebar

Related Questions

i have a problem with data transfert in boost::asio. with the following code :
I'm new on boost.asio. I have a problem when I try a simple example.
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have this piece of code as part of a socks5 proxy server implementation.
I have problems returning JSON data after using Uploadify. This code works in Firefox,
I'm writing server using boost.asio. I have read and write buffer for each connection
I'm writing some code with boost::asio , using asynchronous TCP connections. I've to admit
I'm working on a multithreaded server in c++ using boost-asio. Currently a design problem
I have a problem concerning boost asio libraries. I successfully tried to create a
Have problems with memory have code that looks like this: Have EXC_BAS_ACCESS @interface MapList

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.