I'm trying to create multicast-style SOCK_DGRAM unix .sock
socket where N clients can then connect to and read the messages. OS is GNU/Linux.
Here's my attempt:
package main
import (
"log"
"net"
"os"
"syscall"
"time"
)
func main() {
sockFile := `multicast.sock`
err := syscall.Mkfifo(sockFile, 0o766|syscall.SOCK_DGRAM|syscall.AF_UNIX)
if err != nil {
panic(err)
}
defer os.Remove(sockFile) // Remove sock after exit
fi, err := os.Stat(sockFile)
if err != nil {
panic(err)
}
addr, err := net.ResolveUnixAddr("unixgram", fi.Name())
if err != nil {
panic(err)
}
conn, err := net.ListenUnixgram("unixgram", addr)
if err != nil {
panic(err)
}
defer conn.Close()
for {
wb, err := conn.Write([]byte(`hello, world!`))
if err != nil {
log.Printf(`err: %v`, err)
break
}
if wb == 0 {
log.Printf(`we didn't write anything!`)
}
time.Sleep(time.Second * 1)
}
}
Line conn, err := net.ListenUnixgram("unixgram", addr)
gives error:
panic: listen unixgram multicast.sock: bind: address already in use
There are no other programs trying to connect to it, so how it is already in use? What's the correct way to listen and create the socket file?