使用 Boost.Asio 进行广播的问题

2024-05-05

如果问题之前已得到解答,我提前表示歉意,但我已经搜索并没有找到任何对我有帮助的东西。正如问题标题所示,我正在尝试将包从服务器广播到一组侦听任何消息的客户端。

客户端将计算一秒钟内收到的消息数。

服务器端的事情是这样的:

class Server
{
public:

    Server(boost::asio::io_service& io)
        : socket(io, udp::endpoint(udp::v4(), 8888))
        , broadcastEndpoint(address_v4::broadcast(), 8888)
        , tickHandler(boost::bind(&Server::Tick, this, boost::asio::placeholders::error))
        , timer(io, boost::posix_time::milliseconds(20))
    {
        socket.set_option(boost::asio::socket_base::reuse_address(true));
        socket.set_option(boost::asio::socket_base::broadcast(true));

        timer.async_wait(tickHandler);
    }

private:

    void Tick(const boost::system::error_code&)
    {
        socket.send_to(boost::asio::buffer(buffer), broadcastEndpoint);

        timer.expires_at(timer.expires_at() + boost::posix_time::milliseconds(20));
        timer.async_wait(tickHandler);
    }

private:

    udp::socket socket;
    udp::endpoint broadcastEndpoint;

    boost::function<void(const boost::system::error_code&)> tickHandler;
    boost::asio::deadline_timer timer;

    boost::array<char, 100> buffer;

};

它的初始化和运行方式如下:

int main()
{
    try
    {
        boost::asio::io_service io;
        Server server(io);
        io.run();
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << "\n";
    }

    return 0;
}

这(显然)工作得很好。现在客户来了...

void HandleReceive(const boost::system::error_code&, std::size_t bytes)
{
    std::cout << "Got " << bytes << " bytes\n";
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " <host>\n";
        return 1;
    }

    try
    {
        boost::asio::io_service io;

        udp::resolver resolver(io);
        udp::resolver::query query(udp::v4(), argv[1], "1666");

        udp::endpoint serverEndpoint = *resolver.resolve(query);
        //std::cout << serverEndpoint.address() << "\n";

        udp::socket socket(io);
        socket.open(udp::v4());

        socket.bind(serverEndpoint);

        udp::endpoint senderEndpoint;
        boost::array<char, 300> buffer;

        auto counter = 0;
        auto start = std::chrono::system_clock::now();

        while (true)
        {
            socket.receive_from(boost::asio::buffer(buffer), senderEndpoint);
            ++counter;

            auto current = std::chrono::system_clock::now();
            if (current - start >= std::chrono::seconds(1))
            {
                std::cout << counter << "\n";

                counter = 0;
                start = current;
            }
        }
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << "\n";
    }

当在同一台计算机上运行服务器和客户端时,这有效,但当我在与运行客户端的计算机不同的计算机上运行服务器时,这不起作用。

首先,我必须解析服务器的地址,这对我来说似乎很奇怪。也许我不知道广播到底是如何工作的,但我认为服务器会使用其套接字发送一条消息,并打开广播选项,并且该消息将到达同一网络中的所有套接字。

我读到你应该将客户端的套接字绑定到address_v4::any()地址。我做到了,它不起作用(说一些关于已经使用地址/端口的套接字的信息)。

提前致谢。

PS:我使用的是Windows 8。


我有点惊讶这能在同一台机器上工作。我没想到监听端口 1666 的客户端会接收发送到端口 8888 上的广播地址的数据。

bind() http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/basic_datagram_socket/bind/overload1.html assigns a local endpoint (composed of a local address and port) to the socket. When a socket binds to an endpoint, it specifies that the socket will only receive data sent to the bound address and port. It is often advised to bind to address_v4::any() http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/ip__address_v4/any.html, as this will use all available interfaces for listening. In the case of a system with multiple interfaces (possible multiple NIC cards), binding to a specific interface address will result in the socket only listening to data received from the specified interface[1]. Thus, one might find themselves obtaining an address through resolve() http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/ip__basic_resolver/resolve/overload1.html when the application wants to bind to a specific network interface and wants to support resolving it by providing the IP directly (127.0.0.1) or a name (localhost).

需要注意的是,当绑定到套接字时,端点由地址和地址组成。and港口。这就是我惊讶的原因,它可以在同一台机器上运行。如果服务器正在写入广播:8888,则绑定到端口 1666 的套接字不应接收数据报。尽管如此,这里还是端点和网络的可视化:

                                                               .--------.
                                                              .--------.|
.--------. address: any                         address: any .--------.||
|        | port: any      /                  \    port: 8888 |        |||
| server |-( ----------->| address: broadcast |----------> )-| client ||'
|        |                \    port: 8888    /               |        |'
'--------'                                                   '--------'

服务器绑定到任何地址和任何端口,启用广播选项,并将数据发送到远程端点(广播:8888)。绑定到端口 8888 上的任何地址的客户端都应该收到数据。

一个简单的例子如下。

服务器:

#include <boost/asio.hpp>

int main()
{
  namespace ip = boost::asio::ip;
  boost::asio::io_service io_service;

  // Server binds to any address and any port.
  ip::udp::socket socket(io_service,
                         ip::udp::endpoint(ip::udp::v4(), 0));
  socket.set_option(boost::asio::socket_base::broadcast(true));

  // Broadcast will go to port 8888.
  ip::udp::endpoint broadcast_endpoint(ip::address_v4::broadcast(), 8888);

  // Broadcast data.
  boost::array<char, 4> buffer;
  socket.send_to(boost::asio::buffer(buffer), broadcast_endpoint);
}

客户端:

#include <iostream>

#include <boost/asio.hpp>

int main()
{
  namespace ip = boost::asio::ip;
  boost::asio::io_service io_service;

  // Client binds to any address on port 8888 (the same port on which
  // broadcast data is sent from server).
  ip::udp::socket socket(io_service, 
                         ip::udp::endpoint(ip::udp::v4(), 8888 ));

  ip::udp::endpoint sender_endpoint;

  // Receive data.
  boost::array<char, 4> buffer;
  std::size_t bytes_transferred = 
    socket.receive_from(boost::asio::buffer(buffer), sender_endpoint);

  std::cout << "got " << bytes_transferred << " bytes." << std::endl;
}

当客户端与服务器不在同一位置时,可能会出现各种与网络相关的问题:

  • 验证服务器和客户端之间的连接。
  • 验证防火墙例外。
  • 验证路由设备上的广播支持/例外。
  • 使用网络分析工具,例如Wireshark http://www.wireshark.org/,以验证生存时间 http://en.wikipedia.org/wiki/Time_to_live数据包中的字段足够高,不会在路由过程中被丢弃。

1. On Linux, broadcast datagrams received by an adapter will not be passed to a socket bound to a specific interface, as the datagram's destination is set to the broadcast address. On the other hand, Windows will pass broadcast datagrams received by an adapter to sockets bound to a specific interface.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Boost.Asio 进行广播的问题 的相关文章

  • 这种双重实例是否有害,或者根本没有必要?

    在仔细阅读遗留资源时 我发现了这一点 DataSet myUPC new DataSet myUPC dbconn getDataSet dynSQL Resharper 正确地将其中的 new Dataset 部分 灰显 并建议 删除多余
  • strtok() 和空字段

    我正在将一些 C 结构序列化为字符串 然后将其反序列化strtok 但不幸的是 strtok 不检测空字段 例如 1 2 4 有没有替代功能 在linux上有strsep http www mkssoftware com docs man3
  • 从数组中输入多个数字,每个数字检查是否为整数

    每个人 我希望有人能帮我弄清楚C语言的一些东西 这是我第一次认真地做IT方面的作业 我没有经验 而且我正在电子学习中学习 所以老师的帮助不是很好 我需要用C语言开发控制台应用程序 用户需要输入10个整数 如果插入的数字不是整数 需要输出错误
  • TCP客户端;网络流;异步读取; C#

    请原谅我对任务和异步缺乏了解 使用 TcpClient 类 我正在创建与可用服务器的连接 void async RunClientAsync TcpClient client new TcpClient try await client C
  • lambda 表达式到函数指针的转换

    这是这个问题的后续问题 Lambda 如何作为参数传递 https stackoverflow com questions 3321283 c0x lambda how can i pass as a parameter 据推测 MSDN
  • 析构函数、dispose 和 Finalize 方法之间的区别

    我正在研究垃圾收集器在 C 中的工作原理 我对使用感到困惑Destructor Dispose and Finalize方法 根据我的研究和理解 在我的类中拥有析构函数方法将告诉垃圾收集器以析构函数方法中提到的方式执行垃圾收集 该方法不能在
  • 泛型类上的 DebuggerDisplay

    我在应用时遇到问题DebuggerDisplay泛型类的属性 DebuggerDisplay foo class Foo DebuggerDisplay Bar t class Bar
  • 二元运算符重载、隐式类型转换

    class my bool private bool value public my bool bool value value value explicit operator bool return value friend my boo
  • 使用 INF 文件 C++ 以编程方式安装驱动程序

    这里有人可以告诉我如何安装第 3 方设备驱动程序吗 如果提供了所有必需的文件 即 inf 文件 sys 等 则以编程方式进行 这 该解决方案应运行的最低操作系统是Windows2000 我尝试复制 inf文件放入Win文件夹 INF文件夹和
  • 加载配置文件时发生错误:访问路径 c:\Program Files (x86)\... 被拒绝

    我有一个在 Windows 7 上使用 Visual Studio 2010 中的安装程序部署的应用程序 该程序在 Windows 7 和 XP 上部署并运行良好 但当我在 Windows 8 系统上部署它时 出现有关访问配置文件的错误 该
  • C++ fill() 与 uninitialized_fill()

    您好 我是初学者 我想知道容器的 fill 和 uninitialized fill 之间的区别 我在谷歌上进行了快速搜索 但没有得到很好的答案 有人可以帮助我吗 fill 将值 使用赋值运算符 分配给已构造的对象 uninitialize
  • C语言中的array、&array、&array[0]有什么区别? [复制]

    这个问题在这里已经有答案了 在学习C语言中的数组和指针时 我很困惑 为什么ch ch ch 0 彼此相等 而sptr sptr sptr 0 却不相等 这是我的源代码 int main void char ch 7 1 2 3 4 5 6
  • Bool类型返回规则

    我使用 dapper ORM 所以我使用两个规则Query
  • 向窗口句柄发送消息

    我尝试使用 sendmessage 将消息从我的 C 应用程序传递到 C 我的c 代码是这样的 int tmain int argc TCHAR argv COPYDATASTRUCT cpd cpd dwData 0 LPCWSTR st
  • 如何在 C# 中停止程序进一步执行

    string FirstName Console ReadLine if FirstName Length gt 12 Console WriteLine if FirstName Length lt 3 Console WriteLine
  • Roslyn,通过 hostObject 传递值

    我正在尝试通过 hostObject 发送一个类 但显然它不想工作 using Roslyn Compilers using Roslyn Compilers CSharp using Roslyn Scripting using Rosl
  • 我们可以使用 C# 录制发送到扬声器的声音吗

    我有一个软件 SoundTap Streaming Audio Recorder 它记录发送到扬声器的任何音频 无论流是来自网络还是来自某些文件或麦克风 我可以在桌面应用程序中制作这样的应用程序 以便我可以录制发送到扬声器的流 无论来源如何
  • 如何将这个基于代码的 WPF 工具提示转换为 Silverlight?

    以下工具提示代码适用于WPF 我正在努力让它发挥作用银光 但它给了我这些errors TextBlock does not contain a definition for ToolTip Cursors does not contain
  • int 类型的构造函数

    考虑到成本 这些情况是否相同 case 1 int a 5 case 2 int a 5 case 3 int a a 5 这三种语法是不同的 请耐心等待 我使用用户定义类型而不是 int 稍后我将回到 int T a 5 Direct i
  • 尝试接收 UDP 多播时出现空指针异常

    在尝试了几次让简单的 UDP 多播接收器工作后 我感到很困惑 在我自己的代码无法按预期工作后 我尝试了 vertx 文档中发布的确切示例 DatagramSocket socket vertx createDatagramSocket ne

随机推荐