使用 boost::asio 在同一主机上多播消息

发布于 2024-12-07 01:04:57 字数 1324 浏览 1 评论 0原文

我正在实现发送器/接收器应用程序以在同一主机上进行多播通话。

在我的构造函数中,我有以下代码来设置套接字。

boost::asio::ip::udp::endpoint listenEndpoint(listenAddr, mcastPort);
m_socket.open(listenEndpoint.protocol());
m_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
m_socket.set_option(boost::asio::ip::multicast::enable_loopback(true));
m_socket.set_option(boost::asio::ip::multicast::hops(1));
m_socket.bind(listenEndpoint);

// Join the multicast group
m_socket.set_option(boost::asio::ip::multicast::join_group(mcastAddr));

m_socket.async_receive_from(boost::asio::buffer(m_data, MAX_PTP_MSG_LENGTH),
        m_senderEndpoint, boost::bind(&PtpIpc::HandleReceiveFrom, this, 
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

其中listenAddr是0.0.0.0。

我的发送方法代码如下:

m_socket.async_send_to(boost::asio::buffer(data, size), m_remoteEndpoint,
    boost::bind(&PtpIpc::HandleSendTo, this,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));

其中 m_remoteEndpoint 是多播地址 224.0.1.129 和多播端口 320。

当应用程序 A 和应用程序 B 位于同一主机上时,应用程序 A 似乎没有从应用程序 B 接收多播消息,反之亦然。但是,如果我将应用程序 B 移动到同一子网上的另一台计算机...那么应用程序 A 会听到多播消息并回复应用程序 B,应用程序 B 也可以接收来自应用程序 A 的回复消息。我已启用环回并设置套接字reuse_address 选项。我缺少什么?

I'm implementing sender/receiver applications to talk multicast on the same host.

In my constructor, I have the following code to setup the socket.

boost::asio::ip::udp::endpoint listenEndpoint(listenAddr, mcastPort);
m_socket.open(listenEndpoint.protocol());
m_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
m_socket.set_option(boost::asio::ip::multicast::enable_loopback(true));
m_socket.set_option(boost::asio::ip::multicast::hops(1));
m_socket.bind(listenEndpoint);

// Join the multicast group
m_socket.set_option(boost::asio::ip::multicast::join_group(mcastAddr));

m_socket.async_receive_from(boost::asio::buffer(m_data, MAX_PTP_MSG_LENGTH),
        m_senderEndpoint, boost::bind(&PtpIpc::HandleReceiveFrom, this, 
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

Where listenAddr is 0.0.0.0.

My sending method code is as follows:

m_socket.async_send_to(boost::asio::buffer(data, size), m_remoteEndpoint,
    boost::bind(&PtpIpc::HandleSendTo, this,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));

Where m_remoteEndpoint is multicast address 224.0.1.129 and muticast port 320.

Application A doesn't seem to receive multicast messages from Application B and vice versa when both are on the same host. But if I move Application B to another machine on the same subnet... then Application A hears multicast message and reply back to Application B, which can also receive the reply message from Application A. I've enabled loopback and also set the socket reuse_address option. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-12-14 01:04:57

删除环回选项后会发生什么。我遇到了类似的问题并删除它修复了它。

What happens when you remove the loopback option. I've had a similar issue and removing that fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文