kangsouth
kangsouth

Reputation: 73

How to use golang to implement multicast over Internet?

I'm a new person in golang programming and multicast programming.I saw a program about golang multicast in https://gist.github.com/fiorix/9664255.

import (
    "encoding/hex"
    "log"
    "net"
    "time"
)

const (
    srvAddr         = "224.0.0.1:9999"
    maxDatagramSize = 8192
)

//send multicast data
func ping(a string) {
    addr, err := net.ResolveUDPAddr("udp", a)
    if err != nil {
        log.Fatal(err)
    }
    c, err := net.DialUDP("udp", nil, addr)
    for {
        c.Write([]byte("hello, world\n"))
        time.Sleep(1 * time.Second)
    }
}

//print received data
func msgHandler(src *net.UDPAddr, n int, b []byte) {
    log.Println(n, "bytes read from", src)
    log.Println(hex.Dump(b[:n]))
}

//join multicast group and receive multicast data
func serveMulticastUDP(a string, h func(*net.UDPAddr, int, []byte)) {
    addr, err := net.ResolveUDPAddr("udp", a)
    if err != nil {
        log.Fatal(err)
    }
    l, err := net.ListenMulticastUDP("udp", nil, addr)
    l.SetReadBuffer(maxDatagramSize)
    for {
        b := make([]byte, maxDatagramSize)
        n, src, err := l.ReadFromUDP(b)
        if err != nil {
            log.Fatal("ReadFromUDP failed:", err)
        }
        h(src, n, b)
    }
}

I ran the code on several computers in a University lab LAN.One computer ran function ping to send multicast packets and others ran function serveMulticastUDP to receive the Multicast packets.It seccessed. But when I ran function ping on one computer and ran function serveMulticastUDP on an other computer in the Internet(two computers are not in a LAN),it could not receive the data.The receiver has a NAT and did not have a public IP address.

I saw some people's answer that Internet don't support multicast.But as I know that VOIP is an example of multicast technology,we can implement multicast in the Internet.

So what's the reason that the receiver couldn't receive packets?What should I do to modify the code to implement multicast over Internet?

Upvotes: 1

Views: 2634

Answers (1)

Ron Maupin
Ron Maupin

Reputation: 6452

Multicast routing is very different from unicast routing. Multicast is a form of broadcast, and it normally doesn't cross routers. You can enable multicast routing, but it must be enabled (using the same multicast routing type) on all the routers in a path, and that simply cannot happen on the public Internet. The public Internet would collapse from dense mode multicast, and no ISPs are going to dedicate resources to sparse mode.

I'm not sure why you think VoIP uses multicast (it doesn't).

If you want to multicast from one site to another site across the public Internet, then you must use a tunnel that supports multicast, e.g. GRE. This will wrap your multicast packets inside unicast packets to cross the public Internet. You must still configure the routers on each side to support multicast routing.

Upvotes: 9

Related Questions