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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:51:08+00:00 2026-06-07T20:51:08+00:00

I have a problem, i have a TCP connection between a client and a

  • 0

I have a problem, i have a TCP connection between a client and a server , when the client initialize he send a message to the server and the serveur answer by a welcom message .

All this work fine on a local network.

So my problem is that I use async_write and async_read ( because I need my server to be asynchronous )
My client send the message to the server , the server see it and answer but my client never get the welcom message .

Otherwise when I close my server , the client received the welcome message .

here is my server code :

main.cpp

int main()
{
  try
  {
  boost::asio::io_service io_service;

  tcp_server server(io_service, 7171);
  io_service.run();
}
 catch (std::exception& e)
{
  std::cerr << e.what() << std::endl;
}

return 0;
}

tcp_server

class tcp_server
 {
  public:
 tcp_server(boost::asio::io_service& io_service, int port) // (1)                                                                                                                                                                            
   : m_acceptor(io_service, tcp::endpoint(tcp::v4(), port))
    {
      std::cout << "Port : " << port << std::endl;
      start_accept();
    }

 private:

  void start_accept()
  {
    tcp_connection::pointer new_connection = tcp_connection::create(m_acceptor.io_service());

    m_acceptor.async_accept(new_connection->socket(),
                            boost::bind(&tcp_server::handle_accept, this, new_connection,
                    boost::asio::placeholders::error));
  }

  void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) // (4)                                                                                                                                  
  {
    if (!error)
      {
        std::cout << "Get one client!" << std::endl;
        new_connection->start();
        start_accept(); // (5)                                                                                                                                                                                                               
      }
  }


  tcp::acceptor m_acceptor;
};

tcp_connection

class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
 public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& ios)
  {
    pointer new_connection(new tcp_connection(ios) );
    return new_connection;
  }

  tcp::socket& socket()
    {
      return m_socket;
    }

  void do_read() // (1)                                                                                                                                                                                                                      
  {
    boost::asio::async_read(m_socket, boost::asio::buffer(m_buffer), // (3)                                                                                                                                                                  
                            boost::bind(&tcp_connection::handle_read, shared_from_this(),
                                        boost::asio::placeholders::error)
                            );
  }


  void start()
  {
    m_message = "Welcome on the server \n";

    boost::asio::async_write(m_socket, boost::asio::buffer(m_message),
                             boost::bind(&tcp_connection::handle_write, shared_from_this(),
                                         boost::asio::placeholders::error)
                             );
  }
private:
 tcp_connection(boost::asio::io_service& io_service)
   : m_socket(io_service)
      { }

  void handle_write(const boost::system::error_code& error)
  {
    std::cout << "handle_write : "<< m_message << std::endl;
    if (!error)
      do_read(); // (2)                                                                                                                                                                                                                      
    else
      std::cout << error.message() << std::endl;
  }

  void handle_read(const boost::system::error_code& error) // (6)                                                                                                                                                                            
  {
    std::cout << "handle read" << m_buffer.data() <<std::endl;
     if (!error)
        do_read();
    else
        close();
  }

 void close() // (7)                                                                                                                                                                                                                        
  {
    m_socket.close();
  }

  tcp::socket m_socket;
  std::string m_message;
  boost::array<char, 128> m_buffer;
};

I don’t understand why ?
And How can I avoid 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-07T20:51:09+00:00Added an answer on June 7, 2026 at 8:51 pm

    Please, the manual of async_read:

    This function is used to asynchronously read a certain number of bytes
    of data from a stream. The function call always returns immediately.
    The asynchronous operation will continue until one of the following
    conditions is true:

    • The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes.
    • An error occurred.

    In your case, none of the 2 conditions are satisfied – until the peer closes the socket.

    You should use async_read_some instead (or async_read_until, but it might be a bit more complicated).

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

Sidebar

Related Questions

so I have this TCP connections between my server and client, and anyone can
I have a Client program by tcp connection that send data to a server.
I have a TCP connection , which client is PHP and server is C#
I have problem with receiving an image over TCP socket [.net 4.0] Server: Socket
I have a windows service that exposes a TCP connection (using WCF). This service
How long can I expect a client/server TCP connection to last in the wild?
I built a tcp server based on apache mina 2.0.4, and have some problems
i have problem with autorotate on iphone i set up in all classes -
I have problem with Oracle 9.2 and JMS. I created PL/SQL routine to send
I have a problem with an J2ME client app, that sends data to an

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.