Write to Client UDP Socket in Go
Check the below samples for client/server communication over UDP. The sendResponse routine is for sending response back to client.
udpclient.go
package main
import (
"fmt"
"net"
"bufio"
)
func main() {
p := make([]byte, 2048)
conn, err := net.Dial("udp", "127.0.0.1:1234")
if err != nil {
fmt.Printf("Some error %v", err)
return
}
fmt.Fprintf(conn, "Hi UDP Server, How are you doing?")
_, err = bufio.NewReader(conn).Read(p)
if err == nil {
fmt.Printf("%s\n", p)
} else {
fmt.Printf("Some error %v\n", err)
}
conn.Close()
}
udpserver.go
package main
import (
"fmt"
"net"
)
func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) {
_,err := conn.WriteToUDP([]byte("From server: Hello I got your message "), addr)
if err != nil {
fmt.Printf("Couldn't send response %v", err)
}
}
func main() {
p := make([]byte, 2048)
addr := net.UDPAddr{
Port: 1234,
IP: net.ParseIP("127.0.0.1"),
}
ser, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Printf("Some error %v\n", err)
return
}
for {
_,remoteaddr,err := ser.ReadFromUDP(p)
fmt.Printf("Read a message from %v %s \n", remoteaddr, p)
if err != nil {
fmt.Printf("Some error %v", err)
continue
}
go sendResponse(ser, remoteaddr)
}
}
hello_echo.go
package main
import (
"bufio"
"fmt"
"net"
"time"
)
const proto, addr = "udp", ":8888"
func main() {
go func() {
conn, _ := net.ListenPacket(proto, addr)
buf := make([]byte, 1024)
n, dst, _ := conn.ReadFrom(buf)
fmt.Println("serv recv", string(buf[:n]))
conn.WriteTo(buf, dst)
}()
time.Sleep(1 * time.Second)
conn, _ := net.Dial(proto, addr)
conn.Write([]byte("hello\n"))
buf, _, _ := bufio.NewReader(conn).ReadLine()
fmt.Println("clnt recv", string(buf))
}
The gist is here:
https://gist.github.com/winlinvip/e8665ba888e2fd489ccd5a449fadfa73
server.go
/*
Usage:
go run server.go
See https://gist.github.com/winlinvip/e8665ba888e2fd489ccd5a449fadfa73
See https://stackoverflow.com/a/70576851/17679565
See https://github.com/ossrs/srs/issues/2843
*/
package main
import (
"fmt"
"net"
"os"
"strconv"
)
func main() {
serverPort := 8000
if len(os.Args) > 1 {
if v,err := strconv.Atoi(os.Args[1]); err != nil {
fmt.Printf("Invalid port %v, err %v", os.Args[1], err)
os.Exit(-1)
} else {
serverPort = v
}
}
addr := net.UDPAddr{
Port: serverPort,
IP: net.ParseIP("0.0.0.0"),
}
server, err := net.ListenUDP("udp", &addr)
if err != nil {
fmt.Printf("Listen err %v\n", err)
os.Exit(-1)
}
fmt.Printf("Listen at %v\n", addr.String())
for {
p := make([]byte, 1024)
nn, raddr, err := server.ReadFromUDP(p)
if err != nil {
fmt.Printf("Read err %v", err)
continue
}
msg := p[:nn]
fmt.Printf("Received %v %s\n", raddr, msg)
go func(conn *net.UDPConn, raddr *net.UDPAddr, msg []byte) {
_, err := conn.WriteToUDP([]byte(fmt.Sprintf("Pong: %s", msg)), raddr)
if err != nil {
fmt.Printf("Response err %v", err)
}
}(server, raddr, msg)
}
}
client.go
/*
Usage:
go run client.go
go run client.go 101.201.77.240
See https://gist.github.com/winlinvip/e8665ba888e2fd489ccd5a449fadfa73
See https://stackoverflow.com/a/70576851/17679565
See https://github.com/ossrs/srs/issues/2843
*/
package main
import (
"fmt"
"net"
"os"
"strings"
)
func main() {
serverEP := "127.0.0.1"
if len(os.Args) > 1 {
serverEP = os.Args[1]
}
if !strings.Contains(serverEP, ":") {
serverEP = fmt.Sprintf("%v:8000", serverEP)
}
conn, err := net.Dial("udp", serverEP)
if err != nil {
fmt.Printf("Dial err %v", err)
os.Exit(-1)
}
defer conn.Close()
msg := "Hello, UDP server"
fmt.Printf("Ping: %v\n", msg)
if _, err = conn.Write([]byte(msg)); err != nil {
fmt.Printf("Write err %v", err)
os.Exit(-1)
}
p := make([]byte, 1024)
nn, err := conn.Read(p)
if err != nil {
fmt.Printf("Read err %v\n", err)
os.Exit(-1)
}
fmt.Printf("%v\n", string(p[:nn]))
}