简体   繁体   English

多播环回

[英]multicast loopbacks

I need to write multicast listener on Go.我需要在 Go 上编写多播侦听器。 I faces the problem of twicing packets when I read it.我在阅读时遇到了两次数据包的问题。 It seems that I need to set IP_MULTICAST_LOOP to false.看来我需要将 IP_MULTICAST_LOOP 设置为 false。 It is not easy to do in Go.在 Go 中做到这一点并不容易。

I found this post .我找到了这个帖子 It seems that it should work.似乎它应该工作。 But I still get copies of the same host.但我仍然得到同一主机的副本。 How should it be done?应该怎么做?

ipAddr, err := net.ResolveUDPAddr("udp", groupAddress)
if err != nil {...}

iface, err := net.InterfaceByName("en0")
if err != nil {...}

conn, err := net.ListenPacket("udp4", groupAddress)
if err != nil {...}

pc := ipv4.NewPacketConn(conn)

if err := pc.JoinGroup(iface, ipAddr); err != nil {...}

if err := pc.SetMulticastLoopback(false); err != nil {...}


if loop, err := pc.MulticastLoopback(); err == nil {...}

buf := make([]byte, 1024)
for {

    n, _, addr, err := pc.ReadFrom(buf)
    if err != nil {...}

    fmt.Printf("recv from %v: [%s] \n", addr, buf[:n])
}

The simplest way is to use the ListenMulticastUDP wrapper in the net package, as actually already explained in the other SO answer you point to, How to set IP_MULTICAST_LOOP on multicast UDPConn in Golang .最简单的方法是在net包中使用ListenMulticastUDP包装器,实际上已经在您指向的其他 SO 答案中解释过, 如何在 Golang 中的多播 UDPConn 上设置 IP_MULTICAST_LOOP

If you follow the implementation of ListenMulticastUDP() , you will see that at a certain point it calls setIPv4MulticastLoopback(fd, false) .如果您遵循ListenMulticastUDP()的实现,您会看到它在某个时刻调用setIPv4MulticastLoopback(fd, false)

If you need something more advanced, the documentation of ListenMulticastUDP() suggests to look at https://godoc.org/golang.org/x/net/ipv4 and https://godoc.org/golang.org/x/net/ipv6 , which document extensively how to do multicast in Go.如果您需要更高级的东西, ListenMulticastUDP()的文档建议查看https://godoc.org/golang.org/x/net/ipv4https://godoc.org/golang.org/x/net /ipv6 ,其中详细记录了如何在 Go 中进行多播。

Here is some minimal code (tested on MacOS, but platform-independent) that shows how to use ListenMulticastUDP() :下面是一些显示如何使用ListenMulticastUDP()最小代码(在 MacOS 上测试,但与平台无关ListenMulticastUDP()

func main() {
    // MDNS (https://en.wikipedia.org/wiki/Multicast_DNS)
    groupAddress := "224.0.0.251:5353"
    ifaceName := "en0"
    if err := run(groupAddress, ifaceName); err != nil {
        fmt.Fprintln(os.Stderr, "error:", err)
        os.Exit(1)
    }
}

func run(groupAddr string, ifaceName string) error {
    iface, err := net.InterfaceByName(ifaceName)
    if err != nil {
        return err
    }

    gaddr, err := net.ResolveUDPAddr("udp", groupAddr)
    if err != nil {
        return err
    }

    conn, err := net.ListenMulticastUDP("udp", iface, gaddr)
    if err != nil {
        return err
    }

    buf := make([]byte, 1024)
    for {
        n, addr, err := conn.ReadFromUDP(buf)
        if err != nil {
            return err
        }
        fmt.Printf("recv %4d bytes from %v\n", n, addr)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM