• <small id='pz7pq'></small><noframes id='pz7pq'>

    1. <i id='pz7pq'><tr id='pz7pq'><dt id='pz7pq'><q id='pz7pq'><span id='pz7pq'><b id='pz7pq'><form id='pz7pq'><ins id='pz7pq'></ins><ul id='pz7pq'></ul><sub id='pz7pq'></sub></form><legend id='pz7pq'></legend><bdo id='pz7pq'><pre id='pz7pq'><center id='pz7pq'></center></pre></bdo></b><th id='pz7pq'></th></span></q></dt></tr></i><div id='pz7pq'><tfoot id='pz7pq'></tfoot><dl id='pz7pq'><fieldset id='pz7pq'></fieldset></dl></div>
        <bdo id='pz7pq'></bdo><ul id='pz7pq'></ul>
      <tfoot id='pz7pq'></tfoot>

        <legend id='pz7pq'><style id='pz7pq'><dir id='pz7pq'><q id='pz7pq'></q></dir></style></legend>

        boost::asio UDP 广播

        时间:2023-07-20

          • <legend id='Mz2jx'><style id='Mz2jx'><dir id='Mz2jx'><q id='Mz2jx'></q></dir></style></legend>
              <bdo id='Mz2jx'></bdo><ul id='Mz2jx'></ul>

                    <tbody id='Mz2jx'></tbody>
                  <tfoot id='Mz2jx'></tfoot>

                  <small id='Mz2jx'></small><noframes id='Mz2jx'>

                  <i id='Mz2jx'><tr id='Mz2jx'><dt id='Mz2jx'><q id='Mz2jx'><span id='Mz2jx'><b id='Mz2jx'><form id='Mz2jx'><ins id='Mz2jx'></ins><ul id='Mz2jx'></ul><sub id='Mz2jx'></sub></form><legend id='Mz2jx'></legend><bdo id='Mz2jx'><pre id='Mz2jx'><center id='Mz2jx'></center></pre></bdo></b><th id='Mz2jx'></th></span></q></dt></tr></i><div id='Mz2jx'><tfoot id='Mz2jx'></tfoot><dl id='Mz2jx'><fieldset id='Mz2jx'></fieldset></dl></div>
                  本文介绍了boost::asio UDP 广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 boost::asio 向本地网络中的所有计算机广播 UDP 消息.通过我想出的例子

                  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();
                  }
                  

                  但我的代码在第一个代码块中抛出异常 invalid argument 异常.不过,它对 127.0.0.1 工作正常.我做错了什么?

                  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?

                  推荐答案

                  尝试使用以下代码片段发送 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:

                      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);
                      }
                  

                  这篇关于boost::asio UDP 广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法使用 Boost.Filesystem 链接程序 下一篇:boost自动链接如何做出选择?

                  相关文章

                    • <bdo id='H3Hhf'></bdo><ul id='H3Hhf'></ul>

                    <small id='H3Hhf'></small><noframes id='H3Hhf'>

                  1. <legend id='H3Hhf'><style id='H3Hhf'><dir id='H3Hhf'><q id='H3Hhf'></q></dir></style></legend><tfoot id='H3Hhf'></tfoot>

                  2. <i id='H3Hhf'><tr id='H3Hhf'><dt id='H3Hhf'><q id='H3Hhf'><span id='H3Hhf'><b id='H3Hhf'><form id='H3Hhf'><ins id='H3Hhf'></ins><ul id='H3Hhf'></ul><sub id='H3Hhf'></sub></form><legend id='H3Hhf'></legend><bdo id='H3Hhf'><pre id='H3Hhf'><center id='H3Hhf'></center></pre></bdo></b><th id='H3Hhf'></th></span></q></dt></tr></i><div id='H3Hhf'><tfoot id='H3Hhf'></tfoot><dl id='H3Hhf'><fieldset id='H3Hhf'></fieldset></dl></div>