简体   繁体   English

boost :: asio :: udp异步响应未完全收到

[英]boost::asio::udp async response is not entirely received

Explanation 说明

My program sends an udp datagram to a server. 我的程序将udp数据报发送到服务器。 Using wireshark I see the query and the response from the server (79 Bytes). 使用wireshark,我可以查看查询和来自服务器的响应(79字节)。

Send function 发送功能

void Query::start_send(const std::string data, const QueryType queryType) {
    boost::shared_ptr<std::string> message(new std::string(data));
    socket.async_send_to(boost::asio::buffer(*message), endPoint,
        boost::bind(&Query::handler_send, this, message,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    this->queryType = queryType;
}

Start receive function 开始接收功能

void Query::start_receive() {
    socket.async_receive_from(
        boost::asio::buffer(receiveBuffer, receiveBuffer.max_size()), endPoint,
        boost::bind(&Query::handler_receive, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
}

Receive handler 接收处理程序

void Query::handler_receive(const boost::system::error_code& error, std::size_t bytes_transferred)
{
    if (!error || error == boost::asio::error::message_size) {
        std::cout << "Bytes transferred " << bytes_transferred << std::endl << strlen((char*)this->receiveBuffer.data()) << std::endl;
    }
    else {
        std::cout << "Receive failed" << std::endl << error.value() << std::endl << error.message() << std::endl;
    }
}

Console output 控制台输出

Bytes transferred 79
11

The bytes_transferred value is correct but my response buffer (char vector) "receiveBuffer" contains only 11 Bytes instead of 79 Bytes. bytes_transferred值正确,但是我的响应缓冲区(字符向量)“ receiveBuffer”仅包含11个字节,而不是79个字节。

Wireshark Wireshark

Sending 正在发送

Wireshark发送数据报

Response 响应

Wireshark接收数据报

Your issue is with strlen((char*)this->receiveBuffer.data()) . 您的问题是strlen((char*)this->receiveBuffer.data())

strlen accepts a nul-terminated C-string as input. strlen接受以n终止的C字符串作为输入。 So it stops counting when it encounters the byte 00 . 因此,当遇到字节00时,它将停止计数。

You seem to receive mixed binary/text data. 您似乎收到混合的二进制/文本数据。 You can't simply print it out as a string. 您不能简单地将其打印为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM