go語言unix域套接字發送udp報文
阿新 • • 發佈:2018-02-12
dial ring pack sock world lis add client 語言 /* server.go */
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)
} 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]))
}
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")
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())
}
}
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]))
}
go語言unix域套接字發送udp報文