开发者

C++ boost asio tcp socket reading old data

开发者 https://www.devze.com 2023-02-28 03:02 出处:网络
I\'m a Boost asio newcomer. I\'ve got a tcp server client pair program running, but when I run a new client, that client picks up all the old writes too.I\'m reading/writing with async_read / async_wr

I'm a Boost asio newcomer. I've got a tcp server client pair program running, but when I run a new client, that client picks up all the old writes too. I'm reading/writing with async_read / async_write. So a server and a 开发者_高级运维client may have been running for a while and sent 100~ packets back and forth. When I open a second client and receive on it, I receive all 100 of the old server-sent packets! "localhost" is used for the ip_address.

Is there some way to clear the buffer or something? Perhaps there's another way other than close the old socket and create a new socket?

The read/write calls (not used in this order, just copied both of the calls from where I used them):

boost::array<char, 1024> buf;
boost::array<char, 1024> rbuf;
boost::asio::async_write(socket_, 
    boost::asio::buffer(buf.c_array(), 
    buf.size()),strand_.wrap(boost::bind(&async_tcp_client::handle_send_msg,
    this,boost::asio::placeholders::error)));

boost::asio::async_read(socket_, boost::asio::buffer(rbuf.c_array(), rbuf.size()),
    strand_.wrap(boost::bind(&async_tcp_client::handle_read,
    this, boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred)));


It's not clear to me how your buffers are scoped. You will need a buffer per connection, one each for both reading and writing. You also need to ensure they stay within scope as long as the asynchronous operation is active.

0

精彩评论

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