将超时到udp call recere_从ASIO中应用
Applying a timeout to UDP call receive_from in ASIO
我具有以下ASIO代码,同步读取UDP数据包。问题是,如果没有给定大小的数据包在给定的时间范围内到达(30秒(,我希望rocieve_from函数以某种错误返回以指定冰冷的超时。
for (;;)
{
boost::array<char, 1000> recv_buf;
udp::endpoint remote_endpoint;
asio::error_code error;
socket.receive_from(asio::buffer(recv_buf), // <-- require timeout
remote_endpoint, 0, error);
if (error && error != asio::error::message_size)
throw asio::system_error(error);
std::string message = make_daytime_string();
asio::error_code ignored_error;
socket.send_to(asio::buffer(message),
remote_endpoint, 0, ignored_error);
}
查看未面向UDP的呼叫的文档支持超时机制。
在ASIO中与Syncronous UDP访问的时间是什么正确的方法(如果可能的话(?
据我所知,这是不可能的。通过运行同步receive_from
,您可以通过#include <sys/socket.h>
封锁SYSCALL recvmsg
执行代码。
随着可移植性的发展,我不能为Windows说话,但是Linux/BSD C-Flavourane解决方案看起来像这样:
void SignalHandler(int signal) {
// do what you need to do, possibly informing about timeout and calling exit()
}
...
struct sigaction signal_action;
signal_action.sa_flags = 0;
sigemptyset(&signal_action.sa_mask);
signal_action.sa_handler = SignalHandler;
if (sigaction(SIGALRM, &signal_action, NULL) == -1) {
// handle error
}
...
int timeout_in_seconds = 5;
alarm(timeout_in_seconds);
...
socket.receive_from(asio::buffer(recv_buf), remote_endpoint, 0, error);
...
alarm(0);
如果这根本不可行,我建议您完全异步并在boost::asio::io_service
中运行。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 提升 ASIO 无法识别计时器对象
- 试图在visual studio上用C++创建一个桌面应用程序
- boost::asio::io_service 在线程中,不会在应用程序退出时退出
- 将 std::bind 应用于带有参数 <boost::asio::ip::tcp::socket> 时出错?
- 将超时到udp call recere_从ASIO中应用
- Boost.Asio:由于线程退出或应用程序请求,I/O 操作已中止
- 使用无效套接字调用boost::asio::write()使我的Blackberry 10应用程序崩溃
- 如何将boost.asio添加到windows通用应用程序项目中
- 在boost :: asio应用程序的示例中,我如何重置send_break
- 在 ipv6 链接范围单播 udp 客户端-服务器中应用 boost::asio::ip::udp,有时有效,但有时无效
- boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它可以正常工作,为什么
- 在 WXWIDGET 应用程序中使用 boost ASIO 编译错误
- 应用提升::asio
- Boost.Asio应用程序在创建接收器对象时抛出异常
- 同一网络中两台计算机之间的客户端-服务器应用程序(使用boost::asio)
- 如何在用c++编写的应用程序中指定要使用的特定NIC(boost asio)
- 如何将使用boost::asio的本地c++静态库导入CLI/ c++混合模式应用程序?
- 如何将此Boost ASIO示例应用于我的应用程序
- boost.asio :多端口应用层协议