mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-06 00:25:43 +00:00
28 lines
556 B
Go
28 lines
556 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
func main() {
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9981})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Printf("Local: <%s> \n", conn.LocalAddr().String())
|
|
data := make([]byte, 1024)
|
|
for {
|
|
n, remoteAddr, err := conn.ReadFromUDP(data)
|
|
if err != nil {
|
|
fmt.Printf("error during read: %s", err)
|
|
}
|
|
fmt.Printf("<%s> %s\n", remoteAddr, data[:n])
|
|
_, err = conn.WriteToUDP([]byte("world"), remoteAddr)
|
|
if err != nil {
|
|
fmt.Printf(err.Error())
|
|
}
|
|
}
|
|
}
|