[英]Boost asio receive corrupt message
我做了一個服務器和客戶端異步應用程序。 除我收到的消息外,所有其他方法均正常運行。 我正在將圖像片段發送到字符串中。 但是,當我收到它們時,該字符串已損壞,這與發送的字符串不同。 長度相同,幾乎所有字符。 如果將發送的內容與接收的內容進行比較,我會發現與發送的字符有300個不同的字符。 我正在發送50.000個字符的字符串。 知道可能是什么問題嗎? 大部分代碼是注釋,因此您將在幾秒鍾內理解它。 另外,我縮小了它的大小,以使您更容易閱讀。
我正在發送。
// 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
獲取的指針m_messageQueue
。 因此最終您可以從不適當的緩沖區發送。
2)在接收時,您在堆棧上創建了receiveBuffer
。 當async_read_some
立即返回(幾乎總是)時,由於您退出StartReceiving
調用,所以receiveBuffer
將被破壞。
3)與sendBuffer
相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.