boost::asio UDP broadcasting

后端 未结 1 397
我寻月下人不归
我寻月下人不归 2020-12-06 00:58

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 {
    s         


        
相关标签:
1条回答
  • 2020-12-06 01:40

    Try the following code snippet to send a UDP broadcast, utilizing the ba::ip::address_v4::broadcast() call to get an endpoint:

        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);
        }
    
    0 讨论(0)
提交回复
热议问题