Skip to content

Instantly share code, notes, and snippets.

@miekg
Created May 10, 2017 09:29
Show Gist options
  • Save miekg/d9bc045c89578f3cc66a214488e68227 to your computer and use it in GitHub Desktop.
Save miekg/d9bc045c89578f3cc66a214488e68227 to your computer and use it in GitHub Desktop.
Simple udp server in Go
package main
import (
"log"
"net"
)
func main() {
// listen to incoming udp packets
pc, err := net.ListenPacket("udp", ":1053")
if err != nil {
log.Fatal(err)
}
defer pc.Close()
for {
buf := make([]byte, 1024)
n, addr, err := pc.ReadFrom(buf)
if err != nil {
continue
}
go serve(pc, addr, buf[:n])
}
}
func serve(pc net.PacketConn, addr net.Addr, buf []byte) {
// 0 - 1: ID
// 2: QR(1): Opcode(4)
buf[2] |= 0x80 // Set QR bit
pc.WriteTo(buf, addr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment