/* server.go */
package main
import ( "fmt" "net" "os")func checkError(err error) { if err != nil { fmt.Printf("Error: %s\n", err.Error()) os.Exit(1) }}func recvUnixMsg(conn *net.UnixConn) { var buf [20]byte n, raddr, err := conn.ReadFromUnix(buf[0:]) fmt.Println(raddr) if err != nil { return } fmt.Println("msg is ", string(buf[0:n])) _, err = conn.WriteToUnix([]byte("nice to see u"), raddr) checkError(err)}func main() { laddr, err := net.ResolveUnixAddr("unixgram", "/tmp/unix_gram_sock") checkError(err) conn, err := net.ListenUnixgram("unixgram", laddr) checkError(err) recvUnixMsg(conn)}/* client.so */
package main
import ( "fmt" "net" "os")func checkError(err error) { if err != nil { fmt.Printf("Error: %s\n", err.Error()) os.Exit(1) }}func main() { raddr, err := net.ResolveUnixAddr("unixgram", "/tmp/unix_gram_sock") checkError(err) laddr, err := net.ResolveUnixAddr("unixgram", "/tmp/unix_gram_sock_cli") checkError(err) conn, err := net.DialUnix("unixgram", laddr, raddr) checkError(err) defer conn.Close() if err != nil { os.Exit(1) } n, err := conn.Write([]byte("Hello world")) fmt.Printf("send msg n:%d\n", n) var msg [20]byte conn.Read(msg[0:]) fmt.Println("msg is", string(msg[0:10]))}