2017-05-04 106 views
1

我有一個雙多路廣播設置,其中每個多播組需要連接到我服務器上的特定接口。boost :: asio加入錯誤的接口

Coliru

#include <boost/asio.hpp> 
#include <iostream> 

namespace ip = boost::asio::ip; 
using ip::udp; 

boost::asio::io_service io; 

struct Connection { 
    int32_t timeout = 5000; 
    udp::socket sock {io}; 
    ip::address addr; 

    bool Connect(std::string const& localAddr, std::string const& addrStr, int port, boost::system::error_code& ec) { 
     // Multicast socket 
     udp::endpoint local(ip::address::from_string(localAddr), port); // leaving host/port unbound doesn't seem to help 

     std::cout << "Using local " << local << "\n"; 
     addr = ip::address::from_string(addrStr); 
     udp::endpoint multicastEndpoint(addr, port); 

     sock.open(multicastEndpoint.protocol()); 
     // The commented flags don't seem to have any effect on the findings 
     //sock.set_option(ip::multicast::enable_loopback()); 
     sock.bind(local, ec); 
     sock.set_option(ip::multicast::join_group(addr.to_v4())); 
     sock.set_option(udp::socket::reuse_address(true)); 
     //setsockopt(sock.native(), SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)); 

     return ec? false : true; 
    } 
}; 

struct ConnectionPair { 
    Connection a, b; 

    bool Connect(std::string const& addrStrA, int portA, std::string const& addrStrB, int portB, boost::system::error_code& ec) { 
     // Example adresses; Replace with your local adapter addresses 
     return a.Connect("172.17.0.1",  addrStrA, portA, ec) 
      && b.Connect("192.168.195.62", addrStrB, portB, ec); 
    } 
}; 

int main() { 
    try { 
     ConnectionPair pair; 
     boost::system::error_code ec; 

     // all hosts multicast groups 
     if (pair.Connect("224.0.0.251", 5656, "224.0.0.1", 5657, ec)) { 
      std::cout << "Both connected... "; 
      boost::asio::deadline_timer dlt(io, boost::posix_time::seconds(5)); 
      dlt.wait(); 
     } else { 
      std::cout << "Connection error: " << ec.message() << "\n"; 
     } 
    } catch(std::exception const& e) { 
     std::cout << "Exception: '" << e.what() << "'\n"; 
    } 
    std::cout << "Bye\n"; 
} 

問題是,使用這種時進行連接,插座A被沒有得到數據。使用netsh interface ip show join,它顯示兩個多播組都已加入對應於localAddrB的接口中,而不是每個都取代它們。

當使用mdump加入多播組時,每個組連接到正確的位置並接收數據。

我無法弄清楚我的代碼出錯了。

+0

你見過http://stackoverflow.com/questions/10692956/what-does-it-mean-to-bind-a-multicast-udp-socket?所有這些答案似乎都表示它不以這種方式工作。坦率地說,我同意你的看法,儘管在閱讀[§2.4Receiving Multicast Datagrams]時(http://www.tldp.org/HOWTO/Multicast-HOWTO-2.html#ss2.4)。而且我也可以確認它似乎不起作用 - 至少使用Boost 1.62 – sehe

+0

與Boost沒有什麼區別1.64 – sehe

+0

@sehe那麼,如何獲得特定的多播以便通過特定的本地接口接收?這顯然是可能的,因爲mdump做到了。 –

回答

1

您可以指定廣播/加入多播組的接口。

http://www.boost.org/doc/libs/1_47_0/boost/asio/ip/detail/socket_option.hpp

這些2行:

​​

在上下文:

  • 服務器代碼:

    this->socket.open(ip::udp::v4()); 
    this->socket.set_option(ip::udp::socket::reuse_address(true)); 
    this->socket.set_option(ip::multicast::enable_loopback(true)); 
    this->socket.set_option(ip::multicast::hops(HOPS)); 
    
    this->socket.set_option(ip::multicast::outbound_interface(ip::address_v4::from_string(this->address))); 
    this->socket.bind(ip::udp::endpoint(ip::address_v4::from_string(this->address), 0)); // any port 
    
    this->ioService.run(); // blocking 
    
  • 客戶端代碼:

    udp::endpoint bindEndpoint(ip::address_v4::any(), UDPMulticastServer::PORT); 
    this->socket.open(ip::udp::v4()); 
    this->socket.set_option(ip::udp::socket::reuse_address(true)); 
    this->socket.bind(bindEndpoint); 
    
    // Join the multicast group on a specific interface 
    ip::address_v4 joinAddress = ip::address_v4::from_string(UDPMulticastServer::MULTICAST_ADDRESS); 
    ip::address_v4 listenInterface = ip::address_v4::from_string(this->address); 
    this->socket.set_option(ip::multicast::join_group(joinAddress, listenInterface)); 
    
    this->listenForBroadcast(); 
    this->ioService.run(); // blocking