gpt4 book ai didi

c++ - 即使没有传入消息,Boost ASIO UDP 客户端 async_receive_from 也会调用处理程序

转载 作者:搜寻专家 更新时间:2023-10-31 01:42:27 25 4
gpt4 key购买 nike

我已将 Boost 白天客户端教程中的 UDP 客户端代码修改为以下内容:

class UDPClient
{
public:
udp::socket* socket;
udp::endpoint* receiver_endpoint;
boost::array<char, 1024> recv_buffer;

UDPClient();
void do_receive();
void handle_receive(const boost::system::error_code& error, size_t);
};

UDPClient::UDPClient()
{
boost::asio::io_service io_service;

udp::resolver resolver(io_service);
udp::resolver::query query(udp::v4(), "127.0.0.1", "8888");
receiver_endpoint = new udp::endpoint(*resolver.resolve(query));

socket = new udp::socket(io_service);
socket->open(udp::v4());

do_receive();

while (true)
{
io_service.poll();
Sleep(1);
}
}

void UDPClient::do_receive()
{
socket->async_receive_from(boost::asio::buffer(recv_buffer), *receiver_endpoint,
boost::bind(&UDPClient::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void UDPClient::handle_receive(const boost::system::error_code& error, size_t bytes_transferred)
{
cout << "ulala" << endl;

if (!error || error == boost::asio::error::message_size)
do_receive();
}

如果一切按计划进行,只有在有传入消息时才会打印“ulala”。然而,这里不是这种情况......无论是否有传入消息,都会立即调用处理程序并打印“ulala”。我尝试了一些不同的方法:将 io_service.poll() 与 io_service.run() 和 io_service.run_one() 交换,以及删除 Sleep(1),但这些更改都没有对问题产生任何影响。我该怎么办?

提前致谢!

最佳答案

socket = new udp::socket(io_service);
socket->open(udp::v4());

设置一个用于发送到任意端点的套接字。要接收(“收听”),请使用

socket = new udp::socket(io_service, udp::endpoint(udp::v4(), 8888));

除此之外,socket 和 receiver_endpoint 现在已经泄露。要解决这个问题:

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <iostream>

using boost::asio::ip::udp;

class UDPClient
{
public:
boost::asio::io_service io_service;
udp::socket socket;
udp::endpoint receiver_endpoint;
boost::array<char, 1024> recv_buffer;

UDPClient();
void do_receive();
void handle_receive(const boost::system::error_code& error, size_t);
};

UDPClient::UDPClient()
: io_service(),
socket(io_service, {udp::v4(), 8888})
{
do_receive();
io_service.run();
}

void UDPClient::do_receive()
{
socket.async_receive_from(boost::asio::buffer(recv_buffer), receiver_endpoint,
boost::bind(&UDPClient::handle_receive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}

void UDPClient::handle_receive(const boost::system::error_code& error, size_t bytes_transferred)
{
std::cout << "ulala" << std::endl;
std::cout << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";

if (!error || error == boost::asio::error::message_size)
do_receive();
}

int main()
{
UDPClient updclient;
}

这也会打印收到的消息(假设它是可打印的)。见

寻求一种可以同时处理多个 UDP 请求的方法。

关于c++ - 即使没有传入消息,Boost ASIO UDP 客户端 async_receive_from 也会调用处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820215/

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