我正在实施发送方/接收方应用程序以在同一主机上进行多播。
在我的构造函数中,我有以下代码来设置套接字。
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));
Run Code Online (Sandbox Code Playgroud)
其中 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));
Run Code Online (Sandbox Code Playgroud)
其中 m_remoteEndpoint 是多播地址 224.0.1.129 和多播端口 320。
应用程序 A 似乎没有从应用程序 B 接收多播消息,反之亦然,当两者都在同一主机上时。但是如果我将应用程序 B 移动到同一子网上的另一台机器......然后应用程序 A 听到多播消息并回复到应用程序 B,它也可以从应用程序 A 接收回复消息。我启用了环回并设置了套接字重用地址选项。我错过了什么?