C++ 如何向boost UDP服务器添加异步计时器?
我在网上得到了这段代码,并一直在尝试添加一个计时器,以便它每隔一段时间读取一个数据包。我似乎不知道如何将回调函数传递给boost::async\u wait命令,因为我遇到了以下错误:C++ 如何向boost UDP服务器添加异步计时器?,c++,sockets,asynchronous,boost,boost-asio,C++,Sockets,Asynchronous,Boost,Boost Asio,我在网上得到了这段代码,并一直在尝试添加一个计时器,以便它每隔一段时间读取一个数据包。我似乎不知道如何将回调函数传递给boost::async\u wait命令,因为我遇到了以下错误: server1.cpp: In member function ‘void UDPClient::handle_receive(const boost::system::error_code&, size_t)’: server1.cpp:51:66: error: invalid use of non-
server1.cpp: In member function ‘void UDPClient::handle_receive(const boost::system::error_code&, size_t)’:
server1.cpp:51:66: error: invalid use of non-static member function ‘void UDPClient::time_to_receive(const boost::system::error_code&)’
boost::asio::placeholders::error));
^
server1.cpp:33:6: note: declared here
void UDPClient::time_to_receive(const boost::system::error_code& error)
^~~~~~~~~
UDPClient类:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::asio::ip::udp;
class UDPClient
{
public:
boost::asio::io_service io_service;
udp::socket socket;
udp::endpoint receiver_endpoint;
boost::asio::deadline_timer timer;
boost::array<char, 1024> recv_buffer;
UDPClient();
void time_to_receive(const boost::system::error_code& error);
void do_receive();
void handle_receive(const boost::system::error_code& error, size_t);
};
UDPClient::UDPClient()
: io_service(),
socket(io_service, {udp::v4(), 3643}),
timer(io_service, boost::posix_time::seconds(2))
{
do_receive();
io_service.run();
}
void UDPClient::time_to_receive(const boost::system::error_code& error)
{
do_receive();
}
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 << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";
timer.async_wait(boost::bind(time_to_receive,
boost::asio::placeholders::error));
}
int main()
{
UDPClient updclient;
}
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::udp;
类UDPClient
{
公众:
boost::asio::io_服务io_服务;
udp::套接字;
udp::端点接收器\ U端点;
boost::asio::截止期计时器;
boost::数组recv_缓冲区;
UDPClient();
接收无效时间(const boost::system::error\u code&error);
无效不接收();
无效句柄接收(const boost::system::error\u code&error,size\u t);
};
UDPClient::UDPClient()
:io_服务(),
套接字(io_服务,{udp::v4(),3643}),
计时器(io_服务,boost::posix_时间::秒(2))
{
你收到了吗;
io_service.run();
}
void UDPClient::接收时间(const boost::system::error\u代码和错误)
{
你收到了吗;
}
void UDPClient::do_receive()
{
socket.async_receive_from(boost::asio::buffer(recv_buffer)),receiver_端点,
boost::bind(&UDPClient::handle_receive,这个,
boost::asio::占位符::错误,
boost::asio::占位符::字节(已传输);
}
void UDPClient::handle\u receive(常量boost::system::error\u代码和错误,大小\u t字节\u传输)
{
std::cout将bind
与成员函数一起使用的方式是错误的。请按如下所示使用它:
timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
boost::asio::placeholders::error));
至于为什么会这样,我建议您阅读boost文档
此外,我还修改了代码,使其在不退出的情况下像服务器一样运行
初始化主函数中的io_服务
,并将其引用传递给类
初始化io\u服务\u工作
对象。这是io\u服务
的长期工作源。因此,io\u服务
永远不会从run
函数返回,除非work
对象被销毁
完整资料来源:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::asio::ip::udp;
class UDPClient
{
public:
boost::asio::io_service& io_service;
udp::socket socket;
udp::endpoint receiver_endpoint;
boost::asio::deadline_timer timer;
boost::array<char, 1024> recv_buffer;
UDPClient(boost::asio::io_service&);
void time_to_receive(const boost::system::error_code& error);
void do_receive();
void handle_receive(const boost::system::error_code& error, size_t);
};
UDPClient::UDPClient(boost::asio::io_service& ios)
: io_service(ios),
socket(io_service, {udp::v4(), 3643}),
timer(io_service, boost::posix_time::seconds(2))
{
do_receive();
}
void UDPClient::time_to_receive(const boost::system::error_code& error)
{
do_receive();
}
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 << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";
timer.expires_from_now(boost::posix_time::seconds(2));
timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
boost::asio::placeholders::error));
}
int main()
{
boost::asio::io_service ios;
boost::asio::io_service::work wrk(ios);
UDPClient updclient(ios);
ios.run();
}
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::udp;
类UDPClient
{
公众:
boost::asio::io_服务和io_服务;
udp::套接字;
udp::端点接收器\ U端点;
boost::asio::截止期计时器;
boost::数组recv_缓冲区;
UDPClient(boost::asio::io_服务&);
接收无效时间(const boost::system::error\u code&error);
无效不接收();
无效句柄接收(const boost::system::error\u code&error,size\u t);
};
UDPClient::UDPClient(boost::asio::io\u服务和ios)
:io_服务(ios),
套接字(io_服务,{udp::v4(),3643}),
计时器(io_服务,boost::posix_时间::秒(2))
{
你收到了吗;
}
void UDPClient::接收时间(const boost::system::error\u代码和错误)
{
你收到了吗;
}
void UDPClient::do_receive()
{
socket.async_receive_from(boost::asio::buffer(recv_buffer)),receiver_端点,
boost::bind(&UDPClient::handle_receive,这个,
boost::asio::占位符::错误,
boost::asio::占位符::字节(已传输);
}
void UDPClient::handle\u receive(常量boost::system::error\u代码和错误,大小\u t字节\u传输)
{
std::cout将bind
与成员函数一起使用的方式是错误的。请按如下所示使用它:
timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
boost::asio::placeholders::error));
至于为什么会这样,我建议您阅读boost文档
此外,我还修改了代码,使其在不退出的情况下像服务器一样运行
初始化主函数中的io_服务
,并将其引用传递给类
初始化io\u服务\u工作
对象。这是io\u服务
的长期工作源。因此,io\u服务
永远不会从run
函数返回,除非work
对象被销毁
完整资料来源:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::asio::ip::udp;
class UDPClient
{
public:
boost::asio::io_service& io_service;
udp::socket socket;
udp::endpoint receiver_endpoint;
boost::asio::deadline_timer timer;
boost::array<char, 1024> recv_buffer;
UDPClient(boost::asio::io_service&);
void time_to_receive(const boost::system::error_code& error);
void do_receive();
void handle_receive(const boost::system::error_code& error, size_t);
};
UDPClient::UDPClient(boost::asio::io_service& ios)
: io_service(ios),
socket(io_service, {udp::v4(), 3643}),
timer(io_service, boost::posix_time::seconds(2))
{
do_receive();
}
void UDPClient::time_to_receive(const boost::system::error_code& error)
{
do_receive();
}
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 << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";
timer.expires_from_now(boost::posix_time::seconds(2));
timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
boost::asio::placeholders::error));
}
int main()
{
boost::asio::io_service ios;
boost::asio::io_service::work wrk(ios);
UDPClient updclient(ios);
ios.run();
}
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::udp;
类UDPClient
{
公众:
boost::asio::io_服务和io_服务;
udp::套接字;
udp::端点接收器\ U端点;
boost::asio::截止期计时器;
boost::数组recv_缓冲区;
UDPClient(boost::asio::io_服务&);
接收无效时间(const boost::system::error\u code&error);
无效不接收();
无效句柄接收(const boost::system::error\u code&error,size\u t);
};
UDPClient::UDPClient(boost::asio::io\u服务和ios)
:io_服务(ios),
套接字(io_服务,{udp::v4(),3643}),
计时器(io_服务,boost::posix_时间::秒(2))
{
你收到了吗;
}
void UDPClient::接收时间(const boost::system::error\u代码和错误)
{
你收到了吗;
}
void UDPClient::do_receive()
{
socket.async_receive_from(boost::asio::buffer(recv_buffer)),receiver_端点,
boost::bind(&UDPClient::handle_receive,这个,
boost::asio::占位符::错误,
boost::asio::占位符::字节(已传输);
}
void UDPClient::handle\u receive(常量boost::system::error\u代码和错误,大小\u t字节\u传输)
{
std::cout Wow,谢谢你的留言。我甚至没有意识到哈哈。至于更新,我实际上可以编译,谢谢你。但是,我有一个新问题,异步等待并没有导致服务器等待2秒。我向服务器发送了很多消息,它在接收之间没有延迟地接收每一条消息。哦,好的,我知道了异步等待会在2秒后取消计时器,因此您现在必须从\u调用expires\u?现在它工作了,我测试了如果我使用UDP数据包从客户端向服务器发送垃圾邮件会发生什么情况。即使我每2秒调用异步\u receive\u,它仍然会收到每一个pa