Writing UDP clients/servers
Unlike TCP, UDP is connectionless. That means instead of establishing a connection with another peer and sending data back and forth, you simply send data packets and receive them. There are no delivery or ordering guarantees.
One of the prominent uses of UDP is the Domain Name Service (DNS) protocol. UDP is also the choice for many streaming protocols (voice over IP, video streaming, etc.) where occasional package loss is tolerable. Network monitoring tools also favor UDP.
Despite being connectionless, the UDP networking APIs offer an interface similar to the TCP networking APIs. Here, we will show a simple client-server UDP echo server to demonstrate how these APIs can be used.
How to do it...
The following steps show how to write a UDP server:
- Resolve the UDP address the server will listen on using
net.ResolveUDPAddr
:addr, err := net.ResolveUDPAddr("udp4", *address) if err != nil { ...