5

I'm implementing sender/receiver applications to talk multicast on the same host.

In my constructor, I have the following code to setup the socket.

boost::asio::ip::udp::endpoint listenEndpoint(listenAddr, mcastPort);
m_socket.open(listenEndpoint.protocol());
m_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
m_socket.set_option(boost::asio::ip::multicast::enable_loopback(true));
m_socket.set_option(boost::asio::ip::multicast::hops(1));
m_socket.bind(listenEndpoint);

// Join the multicast group
m_socket.set_option(boost::asio::ip::multicast::join_group(mcastAddr));

m_socket.async_receive_from(boost::asio::buffer(m_data, MAX_PTP_MSG_LENGTH),
        m_senderEndpoint, boost::bind(&PtpIpc::HandleReceiveFrom, this, 
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));

Where listenAddr is 0.0.0.0.

My sending method code is as follows:

m_socket.async_send_to(boost::asio::buffer(data, size), m_remoteEndpoint,
    boost::bind(&PtpIpc::HandleSendTo, this,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred));

Where m_remoteEndpoint is multicast address 224.0.1.129 and muticast port 320.

Application A doesn't seem to receive multicast messages from Application B and vice versa when both are on the same host. But if I move Application B to another machine on the same subnet... then Application A hears multicast message and reply back to Application B, which can also receive the reply message from Application A. I've enabled loopback and also set the socket reuse_address option. What am I missing?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gene H
  • 51
  • 1
  • 2
  • This behavior is pretty common; I've also seen it on completely different OSes such as Tru64 Unix. What OS are you using? – Ben Voigt Sep 27 '11 at 23:43
  • @Ben I'm developing under Timesys with Linux kernel 2.6.29. Thanks! – Gene H Sep 27 '11 at 23:53
  • Are both application A and B joining the multicast group, and both sending and receiving to/from the group? Also, do you have separate sockets for receiving from the multicast group vs. sending to the group? – Dave S Sep 28 '11 at 10:48
  • @Dave: Yes, both application A and B joined the multicast group and sending/receiving to/from the same group. As I mentioned initially, they both talk fine when on separate machines, just doesn't work when on the same host. I use the same socket to send and receive to the group and also tried using separate sockets...but neither way works when on the same machine. – Gene H Sep 28 '11 at 15:50
  • Well, I would recommend trying having separate send/receive sockets, and binding your receive sockets to the multicast address. It's how I usually do it, and I haven't had the problem you describe. – Dave S Sep 29 '11 at 00:14
  • @OP: Did you find a solution? I do have the exactly same problem. I am binding the receive socket to the multicast address. – Florian Wolters Sep 14 '15 at 15:09
  • If you have to use `boost::asio::ip::multicast::enable_loopback(true)` for sender socket as well, no? – Nawaz Feb 06 '21 at 12:16

1 Answers1

0

What happens when you remove the loopback option. I've had a similar issue and removing that fixed it.

bossbarber
  • 870
  • 6
  • 10