14

If I want to test a set of multicast IP programs (sender/receiver) without having to set up the networking, can this be done on the same box? If so, what needs to be setup or done differently?

Andy Faibishenko
  • 631
  • 2
  • 7
  • 13

4 Answers4

15

You may have figured this out already (since the question is now 2 years old) but to do multicast on a single host, you only have to do two things: (1) make sure that your receiving multicast sockets have SO_REUSEADDR set (so that multiple processes can bind the same multicast address) and (2) make sure your sending multicast sockets have IP_MULTICAST_LOOP set (so that packets will be "looped back" to receivers on the same system). If your application uses a single socket for both sending and receiving multicasts, you would set both socket options on it.

int recv_s = socket(AF_INET, SOCK_DGRAM, 0);
int send_s = socket(AF_INET, SOCK_DGRAM, 0);
u_int yes = 1;
setsockopt(recv_s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
setsockopt(send_s, IPPROTO_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes));
Alex Dupuy
  • 5,984
  • 3
  • 39
  • 35
  • Can this be tested on Android as well? – zIronManBox May 23 '14 at 03:38
  • You can send to any multicast address that your system is listening for. For IPv6 ff01::1 is the best choice for same-host only multicast. For IPv4, the all-hosts IP addresses 224.0.0.1 is the easiest to use for testing, but if you want to deploy something you should probably choose another address so that your traffic is not received by hosts uninterested in your traffic. https://en.wikipedia.org/wiki/Multicast_address has more information about IPv4 and IPv6 multicast addresses. – Alex Dupuy Aug 16 '17 at 13:46
  • `IP_MULTICAST_LOOP` defaults to `1`, there is no need to set it. Feel free to test it with getsockopt if you don't believe it. – rustyx Apr 08 '22 at 11:07
0

This may not be what you're looking for, but when I was writing code that used a bunch of broadcast and socket connections and such, I just made two virtual machines in VMWare, booted them off the live CDs, and uploaded my code. If your code runs in Windows, just make two installs of Windows. VMWare places the machines on the same subnet, so communication between them works just fine, broadcast and all. (and I assume multicast, though I didn't have direct experience with that.)

Derek
  • 3,087
  • 1
  • 21
  • 23
0

Some amount of network setup is necessary. If you do not want to create a physical network you can add multiple IP addresses to a single network card. If your machine has more than one network card you can even create a network with just two cards and a hub. Also if your machine has a wireless interface and a wired interface then connecting your machine to your wireless hub via both the wireless and wired interfaces will also get you a network.

Hope one of these ideas helps. Pat O

Pat O
  • 1,344
  • 3
  • 12
  • 27
0

I would say the easiest thing to do would be to setup multiple IPs on your NIC. Just make sure you listen on specifict address and not on all.

HTH

unclepaul84
  • 1,404
  • 8
  • 15