首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux多播后续套接字窃取数据

Linux多播后续套接字窃取数据
EN

Stack Overflow用户
提问于 2015-08-31 18:36:30
回答 1查看 136关注 0票数 0

我试图使用Boost ASIO UDP套接字来多播数据。我相信我有有效的发送者和接收者,因为每件事情都是按照一个发送者和接收者的意愿工作的。但是,当我们现在在同一台机器上有多个接收器时,只有绑定到最后一个地址的接收器才会接收数据。我相信这可能是一个配置问题,但我会提供我所能提供的一切。

网络:

代码语言:javascript
复制
[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)

多播发件人:

代码语言:javascript
复制
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();
                                 });

    }

多播接收机:

代码语言:javascript
复制
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时,我能够在正确的地址中看到适当数量的成员。

这里是我对发送方和接收方的输入:

代码语言:javascript
复制
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_64Boost 1.55.0上运行这个程序。

EN

回答 1

Stack Overflow用户

发布于 2015-09-01 19:05:57

我不知道为什么组播没有在这个设置上工作,但我能够让代码在其他地方工作,所以我想我最好把更改发布给发送方和接收方。

多播发件人:

代码语言:javascript
复制
MulticastSender(const std::string& multicastaddress,
                const unsigned short port) : Port(port),
                                             Endpoint(boost::asio::ip::address::from_string(multicastaddress), port),
                                             Socket(IOService, Endpoint.protocol()),
                                             Work(IOService) {
    auto self = this;
    WorkThread = std::thread([self]()
                             {
                                 self->IOService.run();
                             });

    std::string data = "Test message";
    Socket.async_send_to(boost::asio::buffer(data), Endpoint, boost::bind(&MulticastSender::SendHandler, this, boost::asio::placeholders::error));
}

多播接收机:

代码语言:javascript
复制
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));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32317736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档