UDP Sample with golang
UDP Server
server 收到資料會回傳"message recived!"
UDP Server sample code:
package main
import (
"fmt"
"net"
)
func main() {
src := "0.0.0.0:8001"
listener, err := net.ListenPacket("udp", src)
if err != nil {
fmt.Println(err.Error())
}
defer listener.Close()
fmt.Printf("UDP server start and listening on %s.\n", src)
for {
buf := make([]byte, 1024)
n, addr, err := listener.ReadFrom(buf)
if err != nil {
continue
}
go serve(listener, addr, buf[:n])
}
}
func serve(listener net.PacketConn, addr net.Addr, buf []byte) {
fmt.Printf("%s\t: %s\n", addr, buf)
listener.WriteTo([]byte("message recived!\n"), addr)
}
UDP Client
對Server說"hello"
UDP Client sample code:
package main
import (
"fmt"
"net"
"time"
)
func main() {
res, err := sendUDP("127.0.0.1:8001", "hello")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(res)
}
}
func sendUDP(addr, msg string) (string, error) {
conn, _ := net.Dial("udp", addr)
// send to socket
_, err := conn.Write([]byte(msg))
// listen for reply
bs := make([]byte, 1024)
conn.SetDeadline(time.Now().Add(3 * time.Second))
len, err := conn.Read(bs)
if err != nil {
return "", err
} else {
return string(bs[:len]), err
}
}