boost::asio UDP 广播
我想使用 boost::asio
向本地网络中的所有计算机广播 UDP 消息。完成我提出的示例,
try {
socket.open(boost::asio::ip::udp::v4());
boost::asio::socket_base::broadcast option(true);
socket.set_option(option);
endpoint = boost::asio::ip::udp::endpoint(
boost::asio::ip::address::from_string("192.168.1.255"),
port);
}
catch(std::exception &e) {
}
并希望从队列中广播消息,
while(!queue.empty()) {
std::string message = queue.front();
boost::system::error_code ignored_error;
socket.send_to(
boost::asio::buffer(message),
endpoint,
0, ignored_error);
queue.pop_front();
}
但我的代码在第一个代码块中抛出异常 invalid argument
异常。不过对于 127.0.0.1
来说它工作得很好。我做错了什么?
I want to broadcast UDP messages to all computers in a local network using boost::asio
. Working through the examples I came up with
try {
socket.open(boost::asio::ip::udp::v4());
boost::asio::socket_base::broadcast option(true);
socket.set_option(option);
endpoint = boost::asio::ip::udp::endpoint(
boost::asio::ip::address::from_string("192.168.1.255"),
port);
}
catch(std::exception &e) {
}
and want to broadcast messages from my queue with
while(!queue.empty()) {
std::string message = queue.front();
boost::system::error_code ignored_error;
socket.send_to(
boost::asio::buffer(message),
endpoint,
0, ignored_error);
queue.pop_front();
}
but my code throws an exception invalid argument
exception in the first code block. It works fine for 127.0.0.1
though. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用以下代码片段发送 UDP 广播,利用 ba::ip::address_v4::broadcast() 调用来获取端点:
Try the following code snippet to send a UDP broadcast, utilizing the
ba::ip::address_v4::broadcast()
call to get an endpoint: