Boost asio收到损坏的消息
Boost asio receive corrupt message
我成为了一名服务器&客户端异步应用程序。除了我收到的信息外,一切都很顺利。我正在把图像片段串起来。但当我收到它们时,字符串已损坏,我认为它与我发送的不一样。长度是一样的,几乎所有的字符。如果我将我发送的内容与收到的内容进行比较,我会与发送的内容有大约300个字符的不同。我正在发送50000个字符的字符串。你知道问题出在哪里吗?大部分代码都是注释,所以你会在几秒钟内理解它。此外,我缩小了它,让你更容易阅读。
我随此发送。
// Send a message
void StartSendMessage ( MessagePtr msg )
{
// As long as the queue is not empty, the 'sending agent' is still alive
bool writeInProgress =! m_messageQueue.empty() ;
// Queue the message
m_messageQueue.push ( msg ) ;
if ( msg -> BodyLength() != 0 )
{
std:: cout << "Sending :" << msg -> BodyLength() << std:: endl ;
}
// If the 'sending agent' is inactive, start it
if ( !writeInProgress )
{
// Send message asynchronously. We leave the message on the queue
// since it needs to be available during the async read
async_write ( m_socket , boost::asio::buffer ( msg -> HeaderData() , msg -> SendLength () ) ,
boost::bind ( &ASyncConnectionMT::HandleSentMessage , this , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) ) ;
}
}
// Message was sent
void HandleSentMessage ( const boost::system::error_code& ec , size_t size )
{
// Check the error code
if ( ec )
{
// Transfer error
std:: cout << "Error sending message: " << ec.message() << std:: endl ;
DoStop() ;
return ;
}
// Remove the sent message from queue
m_messageQueue.pop() ;
// If the que is not empty, send next message asynchronously.
// We leave the message on the que since it needs to be available during the async send
if ( !m_messageQueue.empty() )
{
MessagePtr msg = m_messageQueue.front() ;
std:: cout << "Message send lenght "<< msg->SendLength() ;
async_write ( m_socket , boost::asio::buffer ( msg -> HeaderData() , msg -> SendLength () ) ,
boost::bind ( &ASyncConnectionMT:: HandleSentMessage , this , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) ) ;
}
}
我正在用这个阅读。
void StartReceiving()
{
// Create receive buffer
BufferPtr receiveBuffer ( new Buffer ) ;
// Start async read, must pass 'this' as shared_ptr, else the
// 'this' object will be destroyed after leaving this function
m_socket.async_read_some ( boost::asio::buffer ( *receiveBuffer ) , boost::bind ( &ASyncConnectionMT::HandleReceivedd , shared_from_this() , receiveBuffer ,
boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) );
}
// Handle received data
void HandleReceivedd ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)
{
if ( !ec )
{
BufferPtr sendBuffer ( new Buffer ) ;
std:: cout << m_socket.remote_endpoint() << ": Message received: " << std:: string (receiveBuffer -> data() , size ) << std:: endl << std:: endl;
std:: cout << "Message lenght received " << size << std:: endl;
// Start receiving next bit
StartReceiving() ;
}
else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}
}
我在这段代码中看到了几个问题:
1) 发送时,您正在将msg
的副本放入m_messageQueue
。但是,当您调用async_write
时,您的缓冲区是由取自msg
的指针构建的,而不是m_messageQueue
。所以最终你可以从错误的缓冲区发送。
2) 收到后,您将在堆栈上创建receiveBuffer
。当async_read_some
立即返回时(几乎总是),您的receiveBuffer
将被销毁,因为您退出了StartReceiving
调用。
3) 与sendBuffer
相同
相关文章:
- 为什么会发生堆损坏
- boost::进程间消息队列引发错误
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- 为什么C中的通用链表中存储的数据已损坏
- ROS2 动态消息模板
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 自定义异常中的消息已损坏
- qlocalsocket到qlocalserver消息在转移期间被损坏
- 错误消息glibc检测到Malloc():内存损坏(快速)
- Boost asio收到损坏的消息
- boost::线程示例和堆损坏消息
- ZeroMQ消息损坏数据