views:

54

answers:

2

Good day.

I'm trying to implement a question - answer logic using boost::asio.

On the Client I have:

        void Send_Message()
        {
         ....
         boost::asio::async_write(server_socket, boost::asio::buffer(&Message, sizeof(Message)), boost::bind(&Client::Handle_Write_Message, this, boost::asio::placeholders::error));
         ....
        }


        void Handle_Write_Message(const boost::system::error_code& error)
        {
         ....
         std::cout << "Message was sent.\n";
         ....
         boost::asio::async_read(server_socket_,boost::asio::buffer(&Message, sizeof(Message)), boost::bind(&Client::Handle_Read_Message, this, boost::asio::placeholders::error));
         ....
        }

        void Handle_Read_Message(const boost::system::error_code& error)
        {
         ....
         std::cout << "I have a new message.\n";
         ....
        }

And on the Server i have the "same - logic" code:

 void Read_Message()
    {
     ....
     boost::asio::async_read(client_socket, boost::asio::buffer(&Message, sizeof(Message)), boost::bind(&Server::Handle_Read_Message, this, boost::asio::placeholders::error));
     ....
    }


    void Handle_Read_Message(const boost::system::error_code& error)
    {
     ....
     std::cout << "I have a new message.\n";
     ....
     boost::asio::async_write(client_socket_,boost::asio::buffer(&Message, sizeof(Message)), boost::bind(&Server::Handle_Write_Message, this, boost::asio::placeholders::error));
     ....
    }


    void Handle_Write_Message(const boost::system::error_code& error)
    {
     ....
     std::cout << "Message was sent back.\n";
     ....
    }

Message it's just a structure.

And the output on the Client is: Message was sent. Output on the Server is: I have a new message.

And that's all. After this both programs are still working but nothing happens. I tried to implement code like:

if (!error)
{
 ....
}
else
{
 // close sockets and etc.
}

But there are no errors in reading or writing. Both programs are just running normally, but doesn't interact with each other. This code is quite obvious but i can't understand why it's not working.

Thanks in advance for any advice.

A: 

Is the underlying socket a unix or inet socket?

You might want to try a tool like tcpdump or wireshark to watch the exchange between the client and server.

wash
A: 

I always forget to call run() or poll() on my boost::asio::io_service. Are you calling that? Because boost requires that called periodically in order to send/receive.

The io_service is running as in the example of boost::asio: boost::thread t(boost::bind(
Max