我在localhost上使用多播UDP来实现在一台机器上运行的松散的协作程序集合.以下代码适用于Mac OSX,Windows和Linux.缺点是代码也会在localhost网络之外接收UDP数据包.例如,sendSock.sendto(pkt, ('192.168.0.25', 1600))
当我从我网络上的另一个盒子发送时,我的测试机器会收到它.
import platform, time, socket, select addr = ("239.255.2.9", 1600) sendSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 24) sendSock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton("127.0.0.1")) recvSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) if hasattr(socket, 'SO_REUSEPORT'): recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True) recvSock.bind(("0.0.0.0", addr[1])) status = recvSock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(addr[0]) + socket.inet_aton("127.0.0.1")); while 1: pkt = "Hello host: {1} time: {0}".format(time.ctime(), platform.node()) print "SEND to: {0} data: {1}".format(addr, pkt) r = sendSock.sendto(pkt, addr) while select.select([recvSock], [], [], 0)[0]: data, fromAddr = recvSock.recvfrom(1024) print "RECV from: {0} data: {1}".format(fromAddr, data) time.sleep(2)
我试图recvSock.bind(("127.0.0.1", addr[1]))
,但这可以防止套接字接收任何多播流量.是否有正确的方法将recvSock配置为仅接受来自127/24网络的多播数据包,或者是否需要测试每个接收数据包的地址?
与此处其他答案中所述相反,IPv4支持基于TTL的多播作用域,如下所示:
0: node-local (not forwarded outside the current host) 1: link-local (not forwarded outside the current subnet) < 32: site-local < 64: region-local < 128: continent-local < 255: global
(它还支持Administratively Scoped Multicast.)
来源:WR史蒂文斯,unix网络编程, 2 次版,第I卷,第19.2,与修正以符合RFC 2365.