Files
netstack/cmd/udp_client/main.go

41 lines
723 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"flag"
"log"
"net"
)
func main() {
var (
addr = flag.String("a", "192.168.1.1:9999", "udp dst address")
)
log.SetFlags(log.Lshortfile | log.LstdFlags)
var err error
udpAddr, err := net.ResolveUDPAddr("udp", *addr)
if err != nil {
panic(err)
}
// 建立UDP连接只是填息了目的IP和端口并未真正的建立连接
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
panic(err)
}
send := []byte("hello world")
if _, err := conn.Write(send); err != nil {
panic(err)
}
log.Printf("send: %s", string(send))
recv := make([]byte, 32)
rn, _, err := conn.ReadFrom(recv)
if err != nil {
panic(err)
}
log.Printf("recv: %s", string(recv[:rn]))
}