我有以下两个发送和接收数据包的功能。
void send(std::string protocol)
{
char *request=new char[protocol.size()+1];
request[protocol.size()] = 0;
memcpy(request,protocol.c_str(),protocol.size());
request_length = std::strlen(request);
boost::asio::write(s, boost::asio::buffer(request, request_length));
}
void receive()
{
char reply[max_length];
size_t reply_length = boost::asio::read(s, boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
问题与这部分有关,boost::asio::buffer(reply, request_length)
其中请求长度是在发送数据包时最初设置的字符串的长度。如何在不知道的情况下检查缓冲区的大小request_length
?另一个问题是如何防止缓冲区溢出?