gf/.example/net/gudp/udp_server.go

30 lines
539 B
Go

package main
import (
"fmt"
"net"
)
func main() {
listener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8999})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Local:", listener.LocalAddr().String())
data := make([]byte, 1024)
for {
n, remoteAddr, err := listener.ReadFromUDP(data)
if err != nil {
fmt.Println(err)
}
fmt.Println(remoteAddr, string(data[:n]))
_, err = listener.WriteToUDP([]byte("world"), remoteAddr)
if err != nil {
fmt.Printf(err.Error())
}
}
}