boost::asio - peeking into a socket buffer(boost::asio - 窥视套接字缓冲区)
问题描述
我使用 boost::asio::read (或者可能是等效的 async_read) 从套接字读取一些数据.
I use boost::asio::read (or may be the equivalent async_read) to read some data from a socket.
是否可以将读取的字节保留在底层套接字中,以便下次在套接字上调用 read 时再次收到该数据?
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 ?
推荐答案
就像 Simon 说的,你不能用 boost::asio::read()
(或 boost::asio::async_read()
).但是,对于 read()
您可以在套接字上调用 native_handle()
以获取套接字描述符,然后将 ::recvmsg()
与MSG_PEEK
标志.同样,您可以使用 null_buffers()
作为接收缓冲区调用 async_read()
,然后使用 native_handle()
/::recvmsg()
技巧来查看数据.查看 本节关于如何使用 null_buffers() 的 boost 文档.
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().
这篇关于boost::asio - 窥视套接字缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::asio - 窥视套接字缓冲区


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01