Last active
June 20, 2023 08:58
-
-
Save kehiy/ff802e174ebe58b7b7588b5ff7705ada to your computer and use it in GitHub Desktop.
simple UDP server in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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