The User Datagram Protocol (UDP) is one of the essential protocols of the internet. This recipe will show you how to listen for the UDP packets and read the content.
Creating the UDP server
How to do it...
- Open the console and create the folder chapter09/recipe02.
- Navigate to the directory.
- Create the serverudp.go file with the following content:
package main
import (
"fmt"
"log"
"net"
)
func main() {
pc, err := net.ListenPacket("udp", ":7070")
if err != nil {
log.Fatal(err)
}
defer pc.Close()
buffer := make([]byte, 2048)
fmt.Println("Waiting for client...