2

I am a beginner of multicast programming. I am using boost::asio to scribe some multicast data.

I wrote a program with the code

boost::array<char,1500>              _receiveBuf;

void WaitForNextRead()
{
  _receiveSocket->async_receive_from(
      boost::asio::buffer(_receiveBuf, 1500),
      _receiveEndPoint,
      boost::bind(
        &AsyncReadHandler,
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
}

void AsyncReadHandler(
  const boost::system::error_code& error, // Result of operation.
  std::size_t bytes_transferred           // Number of bytes received.
)
{
  std::cout << _receiveEndPoint.address() << ":" << _receiveEndPoint.port() << ":" << std::string(_receiveBuf.c_array(), bytes_transferred) << "\n";
  WaitForNextRead();
}

int main()
{
  std::string address;
  int port;
  std::cin >> address;
  std::cin >> port;

  boost::asio::io_service ioService;
  _receiveSocket = new udp::socket( ioService );
  _receiveSocket->open( udp::v4() );      
  _receiveSocket->set_option( udp::socket::reuse_address(true) );
  _receiveSocket->bind( udp::endpoint( address::from_string("0.0.0.0"), port ) );
  _receiveSocket->set_option( multicast::join_group( address::from_string(address) ) );
  _receiveEndPoint.address(address::from_string(address));
  _receiveEndPoint.port(port);

  WaitForNextRead();
  ioService.run();
  return 0;
}

My instance A is joining: 239.1.1.1:12345 My instance B is joining: 239.1.127.1:12345

It is very weird that both instance A and B will get the message from both address!!

Did I miss out some socket option?

PS: Here is my routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
224.0.0.0       *               240.0.0.0       U     0      0        0 eth1
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Ken Tsang
  • 31
  • 2
  • Does this answer your question? [Receiving multiple multicast feeds on the same port - C, Linux](https://stackoverflow.com/questions/2741611/receiving-multiple-multicast-feeds-on-the-same-port-c-linux) – mpromonet Nov 04 '19 at 13:15

1 Answers1

1

I think I found the answer.

Refer to:
http://man7.org/linux/man-pages/man7/ip.7.html
https://bugzilla.redhat.com/show_bug.cgi?id=231899

Linux has a bug that the broadcast IP_ADD_MEMBERSHIP is a global action to all sockets even when part of another process. We need to set the option IP_MULTICAST_ALL to zero (0) to fix this problem.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Ken Tsang
  • 31
  • 2
  • The comment #6 by Neil Horman on the bug is idiotic. INADDR_ANY has nothing to do with multicast whatsoever. Linux seems to have invented the notion of binding to the multicast address in the first place, and then to be using this misfeature as a justification for other bugs. – user207421 May 30 '18 at 04:18