- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 boost asio 库实现一个 tcp 服务器。在服务器端,我使用asio::async_read_some获取数据,使用asio::write写入数据。服务器代码是这样的。
std::array<char, kBufferSize> buffer_;
std::string ProcessMessage(const std::string& s) {
if (s == "msg1") return "resp1";
if (s == "msg2") return "resp2";
return "";
}
void HandleRead(const boost::system::error_code& ec, size_t size) {
std::string message(buffer_.data(), size);
std::string resp = ProcessMessage(message);
if (!resp.empty()) {
asio::write(socket, boost::asio::buffer(message), WriteCallback);
}
socket.async_read_some(boost::asio::buffer(buffer_));
}
然后我写了一个客户端来测试服务端,代码是这样的
void MessageCallback(const boost::system::error_code& ec, size_t size) {
std::cout << string(buffer_.data(), size) << std::endl;
}
//Init socket
asio::write(socket, boost::asio::buffer("msg1"));
socket.read_some(boost::asio::buffer(buffer_), MessageCallback);
// Or async_read
//socket.async_read_some(boost::asio::buffer(buffer_), MessageCallback);
asio::write(socket, boost::asio::buffer("msg1"));
socket.read_some(boost::asio::buffer(buffer_), MessageCallback);
// Or async_read
//socket.async_read_some(boost::asio::buffer(buffer_), MessageCallback);
如果我运行客户端,代码将在第二个 read_some 处等待,输出为:resp1
。
如果我删除第一个 read_some,输出是 resp1resp2
,这意味着服务器做了正确的事情。
似乎第一个 read_some EAT 第二个响应但不给 MessageCallback 函数响应。
我已经在 What is a message boundary? 阅读了问题,我认为如果这个问题是一个“消息边界”问题,第二个 read_some 应该打印一些东西,因为第一个 read_some 只从 tcp 套接字获取部分流。
我该如何解决这个问题?
更新:我尝试将客户端缓冲区的大小更改为 4,输出将是:
resp
resp
似乎 read_some 函数将做的不仅仅是从套接字读取,我将阅读 boost 代码以找出是否属实。
最佳答案
async_read_some()
成员函数很可能没有按照您的意愿进行,请特别注意文档的备注部分
The read operation may not read all of the requested number of bytes. Consider using the
async_read
function if you need to ensure that the requested amount of data is read before the asynchronous operation completes.
请注意,async_read()
自由函数确实提供了您正在寻找的保证
This operation is implemented in terms of zero or more calls to the stream's
async_read_some
function, and is known as a composed operation. The program must ensure that the stream performs no other read operations (such asasync_read
, the stream'sasync_read_some
function, or any other composed operations that perform reads) until this operation completes.
关于c++ - boost read_some 函数丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691005/
我有一些客户端 asio 代码使用 read_some() 直到读取了字节数。但是通过查看所有示例,我无法弄清楚如何将读取缓冲区组装成包含整个消息的更大缓冲区。我该怎么做? boost::array
我正在使用 boost asio 库实现一个 tcp 服务器。在服务器端,我使用asio::async_read_some获取数据,使用asio::write写入数据。服务器代码是这样的。 std::
我有以下使用 boost asio read_some 函数从 TCP 套接字读取的代码。目前代码是同步的,我需要将其转换为异步版本。问题最初是读取了一些字节以识别数据包类型并获取数据包的长度。然后我
下面的代码绝对有效,但速度不如我预期。 我希望我的程序能够以非常快的速度读取数据。还有另一个商业应用程序连接到同一台服务器并以惊人的速度检索数据。服务器端不是问题。 class A { //.
我开始使用 Boost Asio 的 TCP 套接字。 read_some 和有什么区别和 receive和 write_some 有什么区别?和 send ?谢谢! 最佳答案 据我所知,read_s
我们可以用这样的东西在套接字上超时读取: #include #include //... void http_request::set_result(boost::optional* a, boo
此示例基于书 Boost.Asio C++ Network Programming 中的第 1 章书。我正在尝试使用 Boost.Asio 库构建一个简单的客户端和同步服务器。 这是客户端代码 #de
让我们假设一个客户端持有两个不同的大对象(根据字节大小)并序列化它们,然后发送序列化的对象 使用 boost::asio 通过 TCP/IP 网络连接到服务器。 对于客户端实现,我使用 boost::
我已经使用 boost::asio 实现了一个 TCP 服务器。此服务器使用 basic_stream_socket::read_some 函数读取数据。我知道 read_some 不保证提供的缓冲区
所以通常我们会做这样的事情 socket.read_some(boost::asio::buffer(buffer, buffer_size)); 但是如何让它在读取还没有开始的情况下抛出异常比说 3
我将 Boost.Asio 用作简单的套接字库。 当我打开一个套接字时,我创建了一个线程,该线程不断读取该套接字,并在套接字关闭或发生其他一些错误时返回。 while((read = socket->
我正在尝试构建一个客户端以通过特定协议(protocol)从服务器获取数据。我知道我的代码不是最好的 - 但目前我仍在试验 Boost ASIO 的基本功能。 我想从 TCP-Function 中实现
我是一名优秀的程序员,十分优秀!