Skip to content

Instantly share code, notes, and snippets.

@kehiy
Last active June 20, 2023 08:58
Show Gist options
  • Save kehiy/ff802e174ebe58b7b7588b5ff7705ada to your computer and use it in GitHub Desktop.
Save kehiy/ff802e174ebe58b7b7588b5ff7705ada to your computer and use it in GitHub Desktop.
simple UDP server in golang
package main
import (
"fmt"
"net"
)
func main() {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: 3000,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Printf("server listening %s\n", conn.LocalAddr().String())
for {
message := make([]byte, 20)
n, adrr, err := conn.ReadFromUDP(message[:])
if err != nil {
panic(err)
}
data := string(message[:n])
fmt.Printf("received: %s from %s\n", data, adrr)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment