Developing a UDP client
This section demonstrates how to develop a UDP client that can interact with UDP services. The code of udpC.go
is as follows:
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a host:port string")
return
}
CONNECT := arguments[1]
This is how we get the UDP server details from the user.
s, err := net.ResolveUDPAddr("udp4", CONNECT)
c, err := net.DialUDP("udp4", nil, s)
The previous two lines declare that we are using UDP and that we want to connect to the UDP server that is specified by the return value of net.ResolveUDPAddr()
. The actual connection is initiated using net.DialUDP()
.
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("The UDP server is %s\n", c.RemoteAddr...