Skip to content

Instantly share code, notes, and snippets.

@sethgrid
Created February 6, 2014 15:39
Show Gist options
  • Save sethgrid/8846584 to your computer and use it in GitHub Desktop.
Save sethgrid/8846584 to your computer and use it in GitHub Desktop.
package main
// usage:
// Start udp-echoserv.go and use the commandline tool sendip
// $ sendip -p ipv4 -is 192.168.1.81 -p udp -us 5070 -ud 5000 -d "Hello, World" -v 127.0.0.1
// where -is and -us are arbitrary and -ud is the port used in ResolveUDPAddr
import (
"fmt"
"net"
)
func main() {
comm := make(chan string)
go func(c chan string) {
addr, err := net.ResolveUDPAddr("udp", ":5000")
if err != nil {
fmt.Println("Error", err)
}
l, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println("Error", err)
}
data := make([]byte, 255)
for {
length, packet_addr, err := l.ReadFromUDP(data)
if err != nil {
fmt.Println("Error", err)
}
c <- fmt.Sprintf("UDP Echo: %s (%d) from %s", data, length, packet_addr)
}
}(comm)
for {
select {
case info := <-comm:
fmt.Println(info)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment