boost::asio - peeking into a socket buffer

2019-02-19 16:17发布

I use boost::asio::read (or may be the equivalent async_read) to read some data from a socket.

Is it possible that I leave the bytes read in the underlying socket so that next time I call read on the socket I receive again that data ?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-19 16:32

Like Simon said, you can't do it with boost::asio::read() (or boost::asio::async_read()). However, for read() you could call native_handle() on the socket to get the socket descriptor and then use ::recvmsg() with the MSG_PEEK flag. Similarly, you could call async_read() with null_buffers() as the receive buffer and then use the native_handle()/::recvmsg() trick to peek the data. Check out this section of the boost documentation for how to use null_buffers().

查看更多
走好不送
3楼-- · 2019-02-19 16:51

No, it is not possible - if you want a kind of peek you have to store the peeked bytes by your self.

查看更多
登录 后发表回答