问题
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?
回答1:
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));
回答2:
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.)
回答3:
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
回答4:
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
回答5:
Why not download a trial version of VMWare? It'll take a while, but setup a VM. Then, run x-copies of the VM on your one system (if you can). Each will have a virtual NIC. You can then setup the networking such that they are each reachable within the same network. Then, do your testing.
来源:https://stackoverflow.com/questions/1719156/is-there-a-way-to-test-multicast-ip-on-same-box