Skip to content

Instantly share code, notes, and snippets.

@bsiegert

bsiegert/unixtest.go

Created Nov 18, 2013
Embed
What would you like to do?
Test unix domain sockets in Go.
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"time"
)
const Path = "/tmp/unixtest"
func Listen(l net.Listener) {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Printf("Connected; local=%s, remote=%s", conn.LocalAddr(), conn.RemoteAddr())
fmt.Fprintln(conn, "Hello World!")
conn.Close()
}
func main() {
log.Printf("Dialing %s", Path)
l, err := net.Listen("unix", Path)
if err != nil {
log.Fatal(err)
}
log.Printf("Listening; addr=%s", l.(*net.UnixListener).Addr())
go Listen(l)
time.Sleep(time.Second)
cc, err := net.Dial("unix", Path)
if err != nil {
log.Fatal(err)
}
log.Printf("Client connected; local=%s, remote=%s", cc.LocalAddr(), cc.RemoteAddr())
io.Copy(os.Stdout, cc)
cc.Close()
l.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.