I am trying to do simple UNIX-socket communication with GO. I have an existing service that binds to a named socket on disk, and I want GO to interact with it through this interface. For testing, I used Netcat to create such a named socket
$ nc -vlU /tmp/sock
Bound on /tmp/sock
Listening on /tmp/sock
...
The GO code just opens the socket and writes to it and reads from it in a rate-limited loop
package main
import (
"net"
"fmt"
"time"
)
func main() {
ctl, err := net.Dial("unix", "/tmp/sock")
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
buf := make([]byte, 0, 256)
for {
time.Sleep(time.Second)
ctl.Write([]byte("test write\n"))
n, err := ctl.Read(buf)
fmt.Printf("len:%v, data:%v, err:%v\n", n, buf, err)
}
}
I see the writes coming through in the Netcat console:
$ nc -vlU /tmp/sock
Bound on /tmp/sock
Listening on /tmp/sock
Connection received on /tmp/sock
test write
test write
test write
...
But the GO-side of things just loops endlessly while returning a zero length and no error.
$ go run test.go
len:0, data:[], err:<nil>
len:0, data:[], err:<nil>
len:0, data:[], err:<nil>
len:0, data:[], err:<nil>
len:0, data:[], err:<nil>
...
So, the writes are observed in the Netcat console and are therefore working. But what is going on with the reads? Because the net.Dial
interface allows setting a timeout
, I was expecting the net.Conn.Read
operation to be a blocking one.
What am I doing wrong?