package main
import (
f "fmt"
"net"
)
func main() {
addr, err := net.ResolveUDPAddr("udp", ":4822")
if err != nil {
f.Println(err)
return
}
c, err := net.ListenUDP("udp", addr)
if err != nil {
f.Println(err)
return
}
defer c.Close()
raddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4821")
c.WriteToUDP([]byte("high ! from shareMe"), raddr)
b := make([]byte, 20)
n, ra, err := c.ReadFromUDP(b)
if err != nil {
f.Println(err)
return
}
f.Println("get msg:", ra.String(), string(b[:n]))
}
import (
f "fmt"
"net"
)
func main() {
addr, err := net.ResolveUDPAddr("udp", ":4822")
if err != nil {
f.Println(err)
return
}
c, err := net.ListenUDP("udp", addr)
if err != nil {
f.Println(err)
return
}
defer c.Close()
raddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:4821")
c.WriteToUDP([]byte("high ! from shareMe"), raddr)
b := make([]byte, 20)
n, ra, err := c.ReadFromUDP(b)
if err != nil {
f.Println(err)
return
}
f.Println("get msg:", ra.String(), string(b[:n]))
}
评论
发表评论