c++ boost::asio::async_write发送问题
C++ boost::asio::async_write send problems
当我调用async_write()
时,远端对等端没有接收到数据,直到我再次调用async_write()
。例如,我有3个数据包,a
, b
和c
:
SendPacket(a); // the remote side receives nothing
SendPacket(b); // the remote side receives packet a
SendPacket(c); // the remote side receives packet b
这是我发送的代码:
void Session::SendPacket(packet p)
{
dword len = p.Lenght();
byte* buffer_send = new byte[len + 4]; //4 cause of the header
memcpy(buffer_send + 4, p.GetRaw(), len); // copy everything to the buffer +4, 0-4 is header
m_codec.EncodePacket(buffer_send, len);
boost::asio::async_write(m_socket, boost::asio::buffer(buffer_send, len + 4),
boost::bind(&Session::OnPacketSend, this, len + 4, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, buffer_send));
}
void Session::OnPacketSend(int len, const boost::system::error_code &e, size_t bytes_transferred, byte* buf)
{
// this asynchronously fires when packet data is sent
delete[] buf;
if (e || bytes_transferred != len)
{
Stop();
return;
}
}
我这样使用它:
packet pp;
pp.WriteWord(0);
pp.WriteDword(4);
pp.WriteWord(0);
SendPacket(pp);
同样,当SendPacket()
通过值而不是引用接受packet
时,会发生崩溃。
Gr
当少量数据写入套接字时,例如在原始代码中(12字节~),由于Nagle的算法,通常会观察到数据在后续数据写入套接字之前不被发送的行为。简而言之,许多系统将尝试通过将小的出站消息连接成单个消息然后发送来缓解IP/TCP拥塞。要在每个套接字的基础上显式禁用此行为,请设置boost::asio::ip::tcp::no_delay
选项:
boost::asio::ip::tcp::socket socket(io_service);
// ...
boost::asio::ip::tcp::no_delay option(true);
socket.set_option(option);
如果有足够的带宽,禁用Nagle可能会导致更高的吞吐量。然而,为了确定何时或哪些数据可以被缓冲,以及何时需要立即发送数据,仍然值得更多地检查应用程序协议和逻辑。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- asio::read() 需要很长时间,使用 asio::write 没有问题
- 同一文件上的 seekp()、seekg()、read() 和 write() 有问题