C++ boost::asio UDP广播
我想使用C++ boost::asio UDP广播,c++,boost,udp,boost-asio,broadcast,C++,Boost,Udp,Boost Asio,Broadcast,我想使用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
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();
}
但是我的代码在第一个代码块中抛出异常
无效参数异常。不过,它在127.0.0.1
上运行良好。我做错了什么?尝试以下代码片段发送UDP广播,使用ba::ip::address\u v4::broadcast()
调用获取端点:
bs::error_code error;
ba::ip::udp::socket socket(_impl->_ioService);
socket.open(ba::ip::udp::v4(), error);
if (!error)
{
socket.set_option(ba::ip::udp::socket::reuse_address(true));
socket.set_option(ba::socket_base::broadcast(true));
ba::ip::udp::endpoint senderEndpoint(ba::ip::address_v4::broadcast(), port);
socket.send_to(data, senderEndpoint);
socket.close(error);
}
你怎么能避免向self发送消息?@yonutix Nope,广播的目的是向所有人发送消息(self也包括在内)。