kavaliero
kavaliero

Reputation: 419

boost asynchronous TCP server write error

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 ?

Upvotes: 1

Views: 1508

Answers (1)

Igor R.
Igor R.

Reputation: 15075

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).

Upvotes: 2

Related Questions