Created
May 10, 2017 09:29
-
-
Save miekg/d9bc045c89578f3cc66a214488e68227 to your computer and use it in GitHub Desktop.
Simple udp server in Go
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 ( | |
"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