boost::asio - peeking into a socket buffer

时光毁灭记忆、已成空白 提交于 2019-12-01 08:35:45

问题


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 ?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/9135904/boostasio-peeking-into-a-socket-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!