listen on a socket

This commit is contained in:
Vincent Batts 2018-05-18 09:55:00 -04:00
parent 51aa789e3e
commit cee9fedf2a

36
app.go
View file

@ -1,7 +1,39 @@
package main
import "fmt"
import (
"log"
"net"
)
func main() {
fmt.Println(socketPath)
l, err := net.Listen("unix", socketPath)
if err != nil {
log.Fatal("listen error:", err)
}
for {
fd, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
go echoServer(fd)
}
}
func echoServer(c net.Conn) {
for {
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
return
}
data := buf[0:nr]
println("Server got:", string(data))
_, err = c.Write(data)
if err != nil {
log.Fatal("Write: ", err)
}
}
}