开发者

Boost.Asio async_write/async_read - programs do not communicate properly

开发者 https://www.devze.com 2022-12-30 15:47 出处:网络
Good day. I\'m trying to implement a question - answer logic using boost::asio. On the Client I have: void Send_Message()

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 开发者_StackOverflow社区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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号