Skip to content

Instantly share code, notes, and snippets.

@stanwu
Forked from fiorix/gist:9664255
Created October 13, 2015 16:08
Show Gist options
  • Save stanwu/8833fcfb750596a63767 to your computer and use it in GitHub Desktop.
Save stanwu/8833fcfb750596a63767 to your computer and use it in GitHub Desktop.
Go multicast example
package main
import (
"encoding/hex"
"log"
"net"
"time"
)
const (
srvAddr = "224.0.0.1:9999"
maxDatagramSize = 8192
)
func main() {
go ping(srvAddr)
serveMulticastUDP(srvAddr, msgHandler)
}
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)
}
}
func msgHandler(src *net.UDPAddr, n int, b []byte) {
log.Println(n, "bytes read from", src)
log.Println(hex.Dump(b[:n]))
}
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment