Home >Backend Development >Golang >How to Achieve Bi-Directional Communication Using Unix Sockets in Go?
You're attempting to implement a simple client-server architecture using Unix sockets, but you've encountered a unidirectional data flow issue. Let's delve into the code and address the problem.
In your client code, you're sending messages to the server successfully, but the server fails to respond. This suggests that you're missing a crucial step: reading the server's response. In the revised client code below, we've added a reader goroutine to handle reading data from the server.
// Client code with a reader goroutine package main import ( "io" "log" "net" "time" ) func reader(r io.Reader) { buf := make([]byte, 1024) for { n, err := r.Read(buf[:]) if err != nil { return } println("Client got:", string(buf[0:n])) } } func main() { c, err := net.Dial("unix", "/tmp/echo.sock") if err != nil { panic(err) } defer c.Close() go reader(c) for { _, err := c.Write([]byte("hi")) if err != nil { log.Fatal("write error:", err) break } time.Sleep(1e9) } }
On the server side, your code looks correct. However, it's imperative to handle errors in goroutines and close connections when done, as shown in the modified server code below:
// Server code with error handling and connection closing package main import ( "log" "net" ) func echoServer(c net.Conn) { defer c.Close() // Ensure connection is closed when the goroutine exits for { buf := make([]byte, 512) nr, err := c.Read(buf) if err != nil { log.Fatal("read error:", err) } data := buf[0:nr] _, err = c.Write(data) if err != nil { log.Fatal("write error:", err) } } } func main() { l, err := net.Listen("unix", "/tmp/echo.sock") if err != nil { log.Fatal("listen error:", err) } for { fd, err := l.Accept() if err != nil { log.Fatal("accept error:", err) } go echoServer(fd) } }
With these modifications in place, your client-server communication should now be fully bi-directional, allowing data to flow both ways.
The above is the detailed content of How to Achieve Bi-Directional Communication Using Unix Sockets in Go?. For more information, please follow other related articles on the PHP Chinese website!