我正在尝试使用Boost ASIO UDP套接字来组播数据。我相信我有功能发送器和接收器,因为一切都按照预期的方式与单个发送器和接收器一起工作。但是,当我们现在在同一台机器上有多个接收器时,只有最后一个地址绑定的接收器才会收到数据。我相信这可能是一个配置问题,但我会尽我所能。
网络
[root@dev ~]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:C1:34:B7
inet addr:192.168.6.131 Bcast:192.168.6.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec1:34b7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:25060380 errors:0 dropped:0 overruns:0 frame:0
TX packets:24562809 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2030905679 (1.8 GiB) TX bytes:5249474660 (4.8 GiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:2687213321 errors:0 dropped:0 overruns:0 frame:0
TX packets:2687213321 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1633558802477 (1.4 TiB) TX bytes:1633558802477 (1.4 TiB)
多播发件人:
MulticastSender(const std::string& listenaddress,
const std::string& multicastaddress,
const unsigned short port) : Address(boost::asio::ip::address::from_string(listenaddress)),
Port(port),
Endpoint(Address, port),
Socket(IOService, Endpoint.protocol()),
Work(IOService) {
Socket.set_option(boost::asio::ip::multicast::join_group(boost::asio::ip::address::from_string(multicastaddress).to_v4(), boost::asio::ip::address::from_string(listenaddress).to_v4()));
auto self = this;
WorkThread = std::thread([self]()
{
self->IOService.run();
});
}
多播接收器:
MulticastReceiver(const std::string &listenaddress,
const std::string &multicastaddress,
const unsigned short port) : Socket(IOService),
ListenAddress(listenaddress),
MulticastAddress(multicastaddress),
Work(IOService) {
boost::asio::ip::udp::endpoint listenendpoint(boost::asio::ip::udp::v4(), port);
boost::system::error_code ec;
Socket.open(listenendpoint.protocol(), ec);
Socket.set_option(boost::asio::ip::udp::socket::reuse_address(true), ec);
Socket.set_option(boost::asio::ip::multicast::enable_loopback(true), ec);
Socket.bind(listenendpoint, ec);
Socket.set_option(boost::asio::ip::multicast::join_group(boost::asio::ip::address::from_string(multicastaddress).to_v4(), boost::asio::ip::address::from_string(listenaddress).to_v4()), ec);
auto self = this;
WorkThread = std::thread([self]()
{
self->IOService.run();
});
Socket.async_receive_from(boost::asio::buffer(Buffers[BufferIndex], MaxLength), Endpoint,
boost::bind(&MulticastReceiver::HandleReceiveFrom, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
对于listenaddress
我已尝试同时指定192.168.6.131
的界面,我也尝试将其留给系统0.0.0.0
。我还尝试了join_group
的两种变体,您可以在其中指定接口。
当我运行应用程序时,我能够在运行netstat -gn时在正确的地址中看到正确数量的成员。
以下是我对发件人和收件人的输入:
listenaddress = "0.0.0.0"; // or listenaddress = "192.168.6.131"; // eth1
multicastaddress = "239.140.127.0";
port = 35000;
我在CentOS 6.6 with kernel Linux dev 2.6.32-504.el6.x86_64
上运行此广告并运行Boost 1.55.0
。
答案 0 :(得分:0)
我无法弄清楚为什么多播没有在该设置上工作,但我能够让代码在其他地方工作,所以我想我也可以发布发送方和接收方的更改。
多播发件人:
MulticastReceiver(const std::string &listenaddress,
const std::string &multicastaddress,
const unsigned short port,
const int numbuffers) : Socket(IOService),
ListenAddress(listenaddress),
MulticastAddress(multicastaddress),
Work(IOService) {
boost::asio::ip::udp::endpoint listenendpoint(boost::asio::ip::address::from_string(listenaddress), port);
boost::system::error_code ec;
Socket.open(listenendpoint.protocol(), ec);
Socket.set_option(boost::asio::ip::udp::socket::reuse_address(true), ec);
Socket.set_option(boost::asio::ip::multicast::enable_loopback(true), ec);
Socket.bind(listenendpoint, ec);
Socket.set_option(boost::asio::ip::multicast::join_group(boost::asio::ip::address::from_string(multicastaddress)), ec);
auto self = this;
WorkThread = std::thread([self]()
{
self->IOService.run();
});
Socket.async_receive_from(boost::asio::buffer(Buffers[BufferIndex], MaxLength), Endpoint,
boost::bind(&MulticastReceiver::HandleReceiveFrom, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
多播接收器:
li = [1,2,3,4,5,6,7]