gpt4 book ai didi

c++ - Boost asio 收到损坏的消息

转载 作者:行者123 更新时间:2023-11-28 06:24:44 25 4
gpt4 key购买 nike

我制作了一个服务器和客户端异步应用程序。除了我收到的消息外,一切都完美无缺。我正在将图像片段发送到字符串中。但是当我收到它们时,字符串已损坏,我认为它与我发送的不一样。长度是一样的,而且几乎都是字符。如果我将发送的内容与收到的内容进行比较,我发现与发送的内容有 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) 当您发送时,您将 copy of msg 放入 m_messageQueue。但是,当您调用 async_write 时,您的缓冲区是根据从 msg 获取的指针构建的,而不是 m_messageQueue。所以最终你可以从不正确的缓冲区发送。

2) 在接收时在堆栈上创建 receiveBuffer。当 async_read_some 立即返回(几乎总是)时,您的 receiveBuffer 将被销毁,因为您退出了 StartReceiving 调用。

3) 同sendBuffer

关于c++ - Boost asio 收到损坏的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28723549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com