如何获取升压缓冲区的大小
我正在尝试在 Visual Studio 中创建一个异步服务器,并使用它
boost::asio::async_read(m_socket, boost::asio::buffer(m_buffer),
boost::bind(&tcp_connection::handle_read, shared_from_this(),
boost::asio::placeholders::error));
来将缓冲区放入 m_buffer 中,
boost::array<char, 256> m_buffer;
但是如何获取这个东西 m_buffer 的大小?
size() 不起作用,end() 不起作用..任何帮助都会很棒。提前致谢。
I am trying to make an asynchronised server in visual studio and I use
boost::asio::async_read(m_socket, boost::asio::buffer(m_buffer),
boost::bind(&tcp_connection::handle_read, shared_from_this(),
boost::asio::placeholders::error));
to get the buffer to be put in m_buffer
boost::array<char, 256> m_buffer;
but how do I get the size of this thing, m_buffer?
size() didn't work, end() didn't work.. Any help would be fantastic. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
boost::array
具有恒定的大小。如果要将其打印为以 null 结尾的字符串,请使用.data()
获取const char*
。如果你只是想找到
\0
的位置,使用std::find
。boost::array
has a constant size. If you want to print it as a null-terminated string, use.data()
to get aconst char*
.If you just want to find the position of the
\0
, usestd::find
.boost::arrays 具有基于第二个模板参数的恒定大小。您可以通过调用它们的 size() 方法来检索它们的大小。请参阅 boost::array 文档。
boost::arrays have constant size based on the second template argument. You can retrieve their size by calling their size() method. See boost::array documentation.
如果您将缓冲区的大小指定为 256,然后在缓冲区中的位置
0
处放置一个字符并尝试cout
缓冲区,它将打印整个缓冲区缓冲。这是因为程序无法知道您在缓冲区中只放置了一个“有效”字符。您需要自己在缓冲区中保留一个单独的“指针”,以让您知道数据在哪里结束以及缓冲区的“空”部分开始。If you specified the size of the buffer to be 256, and then you place a character at location
0
in the buffer and try tocout
the buffer, it will print the entire buffer. This is because the program has no way of knowing that you placed only one "valid" character in the buffer. You will need to keep a separate "pointer" into the buffer yourself that lets you know where your data ends and the "empty" part of the buffer begins.