[英]Operation canceled boost asio async_receive_from
I have an UDP Server set up with boost/asio (I copied the example and just changed a few things). 我有一个使用boost / asio设置的UDP服务器(我复制了示例,并做了一些更改)。 Below is the code:
下面是代码:
udp_server.hpp udp_server.hpp
using boost::asio::ip::udp;
class udp_server {
public:
udp_server(boost::asio::io_service&, int);
private:
boost::array<char, 256> recBuffer;
udp::socket socket_;
udp::endpoint remote_endpoint_;
void start_receive();
void handle_receive(const boost::system::error_code&, std::size_t);
void handle_send(boost::shared_ptr<std::string> /*message*/,
const boost::system::error_code& /*error*/,
std::size_t /*bytes_transferred*/)
{}
};
and udp_server.cpp 和udp_server.cpp
udp_server::udp_server( boost::asio::io_service& io_service,
int port)
: socket_(io_service, udp::endpoint(udp::v4(), port)) {
serverNotifications.push_back("UDP Server class initialized.");
start_receive();
}
void udp_server::start_receive() {
socket_.async_receive_from(
boost::asio::buffer(recBuffer),
remote_endpoint_,
boost::bind(&udp_server::handle_receive,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
serverNotifications.push_back("Starting to receive UDP Messages.");
}
void udp_server::handle_receive(const boost::system::error_code& error,
std::size_t size) {
serverNotifications.push_back("RecFrom: " + remote_endpoint_.address().to_string());
if (!error) {
// I do data stuff here
} else {
errors.push_back("Handle Receive error: " + error.message());
}
}
After initializing the Server with: 用以下方法初始化服务器后:
try {
udp_server server(io_service, ApplData.PORT, (size_t)ApplData.BUFLEN);
} catch (std::exception& e) {
// error handling
}
and running it with io_service.run()
in a seperate try catch in another function I get some problems: 并在单独的尝试中使用
io_service.run()
来运行另一个函数,我遇到了一些问题:
handle_receive
gets called without any UDP message getting send in the whole network (aka only my laptop without connection) handle_receive
被调用,而没有在整个网络中发送任何UDP消息(也就是只有我的没有连接的笔记本电脑)
error.message()
returns "Operation canceled" error.message()
返回“操作已取消” remote_endpoint_.address().to_string()
returns "acfc:4000:0:0:7800::%2885986016" which I can't identify as something useful remote_endpoint_.address().to_string()
返回“ acfc:4000:0:0:7800 ::%2885986016”,我无法将其识别为有用的东西 io_service
is stopping all the time, but in my understanding it should run all the time, right? io_service
一直都在停止,但是据我了解,它应该一直都在运行,对吗? I already thought about referencing this in the callback function bind with a shared_from_this ptr
, but since I have a real instance of the udp_server
class until I leave my program I can't think of a good reason to do that. 我已经考虑过在与
shared_from_this ptr
绑定的回调函数中引用此方法,但是由于我拥有udp_server
类的真实实例,直到我离开程序后,才想到这样做的充分理由。
Can someone explain thy this failure occurs, what these errors tell me about my code or what I can do to avoid them? 有人可以解释您发生这种故障的原因吗?这些错误告诉我有关我的代码的信息,或者我可以做些什么来避免它们?
Nevermind, Rubberduck debugging was enough. 没关系,Rubberduck调试就足够了。 I just read the line
我刚刚读了这句话
but since I have a real instance of the udp_server class until I leave my program I can't think of a good reason to do that.
但是由于我在离开程序之前就拥有udp_server类的真实实例,所以我想不出这样做的充分理由。
and noticed, that I actually didn't have this and this was the error. 并注意到,我实际上没有这个,这就是错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.