2017-05-04 1 views
1

각 멀티 캐스트 그룹이 내 서버의 특정 인터페이스에 연결해야하는 이중 멀티 캐스트 설정이 있습니다. 연결하려면이 옵션을 사용하는 경우 Coliruboost :: asio가 잘못된 인터페이스에 합류했습니다.

#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.4 멀티 캐스트 데이터 그램 수신] (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.set_option(ip::multicast::outbound_interface(ip::address_v4::from_string(this->address))); 

this->socket.set_option(ip::multicast::join_group(joinAddress, listenInterface)); 

:

  • 서버 코드 :

    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 
    
관련 문제