Created
November 18, 2013 13:23
-
-
Save bsiegert/7527628 to your computer and use it in GitHub Desktop.
Test unix domain sockets in Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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