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?