Simulating multicasting on loopback interface

2019-06-03 23:31发布

I'm writing a network application in ruby which should use UDP multicasting.

My problem is that I want to run multiple instances for testing purposes on localhost, but multicasting only works if I bind the socket to the real network interface.

Is there some way to enable multicasting for the loopback interface, so that all the 127.0.0.x get the message I send? Currently I enable multicasting with:

ip = IPAddr.new('234.56.78.9').hton + IPAddr.new('0.0.0.0').hton
socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP,ip)
#socket.bind '127.0.0.1',1234 ##does not receive multicast :(
socket.bind '0.0.0.0',1234

Also, I noticed that if I e.g. bind the socket to 127.0.0.4 and send a message, in the packet the source ip is 127.0.0.1 anyway... is there a way to set the source IP so it shows the same IP I bound the socket to?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-06-04 00:06

Solaris allows you to use multicast on the loopback device. For other operating systems you can enable IP_MULTICAST_LOOP on the sender (Unix) or the receiver (Windows) for similar effect.

查看更多
登录 后发表回答