mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
30 lines
539 B
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())
|
|
}
|
|
}
|
|
}
|