...

/

A UNIX Domain Socket Client

A UNIX Domain Socket Client

Let’s learn how to implement a UNIX domain socket client with Go.

We'll cover the following...

This lesson shows a UNIX domain socket client implementation, which can be used to communicate with a domain socket server, such as the one developed in the previous lesson.

Coding example

The relevant code can be found in socketClient.go:

Press + to interact
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"time"
)
func main() {
if len(os.Args) == 1 {
fmt.Println("Need socket path")
return
}
socketPath := os.Args[1]
c, err := net.Dial("unix", socketPath)
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">> ")
text, _ := reader.ReadString('\n')
_, err = c.Write([]byte(text))
if err != nil {
fmt.Println("Write:", err)
break
}
buf := make([]byte, 256)
n, err := c.Read(buf[:])
if err != nil {
fmt.Println(err, n)
return
}
fmt.Print("Read: ", string(buf[0:n]))
if strings.TrimSpace(string(text)) == "STOP" {
fmt.Println("Exiting UNIX domain socket client!")
return
}
time.Sleep(5 * time.Second)
}
}

From lines 1–17, is the part where we get from the user the socket file that is going to be used—the ...