title: Unix Sockets in Go
body: <p>I'm trying to make a simple echo client and server that uses Unix sockets. In this example, the connection seems to be unidirectional. The server can receive data from the client, but it can't send the data back. If I use tcp connections instead, it works great. Does anyone know what's wrong?</p>
  
  <h2>Server</h2>
  
  <pre><code>package main
  
  import "net"
  import "fmt"
  
  func echoServer(c net.Conn) {
      for {
          buf := make([]byte, 512)
          nr, err := c.Read(buf)
          if err != nil {
              return
          }
  
          data := buf[0:nr]
          fmt.Printf("Received: %v", string(data))
          _, err = c.Write(data)
          if err != nil {
              panic("Write: " + err.String())
          }
      }
  }
  
  func main() {
      l, err := net.Listen("unix", "/tmp/echo.sock")
      if err != nil {
          println("listen error", err.String())
          return
      }
  
      for {
          fd, err := l.Accept()
          if err != nil {
              println("accept error", err.String())
              return
          }
  
          go echoServer(fd)
      }
  }
  </code></pre>
  
  <h2>Client</h2>
  
  <pre><code>package main
  
  import "net"
  import "time"
  
  func main() {
      c,err := net.Dial("unix","", "/tmp/echo.sock")
      if err != nil {
          panic(err.String())
      }
      for {
          _,err := c.Write([]byte("hi\n"))
          if err != nil {
              println(err.String())
          }
          time.Sleep(1e9)
      }
  }
  </code></pre>