作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ITNOA
我想编写同时发送和接收多播的程序,如下所示
int main()
{
const string group_address = "235.127.1.1";
constexpr uint16_t PORT_NUMBER = 8765;
boost::asio::io_context io_context;
auto work_guard = boost::asio::make_work_guard(io_context);
BoostMessageSender message_sender(group_address, "eth1", PORT_NUMBER, io_context);
BoostMessageReceiver message_receiver(group_address, "eth1", PORT_NUMBER, io_context);
std::thread t1([&io_context]()
{
cout << __LINE__ << endl;
io_context.run();
});
this_thread::sleep_for(1s);
std::array<char, 20> data = {};
auto result = std::async(std::launch::async, [&message_receiver, &data]()
{
message_receiver.receive(gw::buffer(data));
cout << " receive finished" << endl; // does not reach this line :(((
});
this_thread::sleep_for(1s);
message_sender.send("Any body there!");
result.wait(); // Raise exception what(): std::future_error: No associated state
cout << "receive message: ";
std::copy(std::begin(data),
std::end(data),
std::ostream_iterator<char>(std::cout, ""));
io_context.stop();
t1.join();
return 0;
}
我的问题是为什么result.wait()引发异常?
message_sender.send
行时,该程序不会引发任何异常,并且
result.wait()
块行直到结束。但是当添加
message_sender.send
行时,此程序引发异常,并且无法正常工作。
BoostMessageReceiver::BoostMessageReceiver(const std::string& group_address, const std::string& interface_name, uint16_t port_number, ba::io_context& io_context)
: AbstractMessageReceiver(group_address, interface_name, port_number)
, io_context(io_context)
, listen_endpoint(ba::ip::address_v4::any(), port_number)
, socket(io_context)
{
socket.open(listen_endpoint.protocol());
socket.set_option(ba::ip::udp::socket::reuse_address(true));
socket.bind(listen_endpoint);
// Join to multicast group
socket.set_option(ba::ip::multicast::enable_loopback(true));
socket.set_option(ba::ip::multicast::join_group(ba::ip::address::from_string(group_address)));
}
std::pair<bool, std::size_t> BoostMessageReceiver::receive(const AbstractMessageReceiver::MutableBuffer& buffer) noexcept
{
ba::ip::udp::endpoint sender_endpoint;
std::future<std::size_t> result = socket.async_receive_from(ba::buffer(buffer.data(), buffer.size()), sender_endpoint, ba::use_future);
return std::make_pair(result.get() != 0, result.get());
}
我哪里错了?
Visual Studio 2019 16.7.4
Windows 10 1909 latest update
Boost 1.73.0
Ubuntu 20.04.1 LTS
GCC 9.3.0
Boost 1.71.0
最佳答案
您正在获取no_state
,因为get
上的future
只能被调用一次。
下面
std::make_pair(result.get() != 0, result.get());
get
被调用两次。
Notes The implementations are encouraged to detect the case whenvalid() is false before the call and throw a std::future_error with anerror condition of std::future_errc::no_state.
get
后,
valid
返回
false
。
关于c++ - 使用use_future的多播Boost Asio async_recieve_与async_send_to不能同时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64063109/
我正在浏览 asio 功能 use_future,阅读 source code . 但无法弄清楚它是如何工作的。说,如果我打电话 auto fut = async_write(sock, buffer
我是一名优秀的程序员,十分优秀!