package main import ( "net" "fmt" "time" "sync" ) var hosts = []HostEntry{ {"h.woozle.org:30919", "Ginnie (tablet)"}, {"h.woozle.org:44321", "Ginnie"}, {"h.woozle.org:58000", "Amy"}, {"h.woozle.org:29837", "Neale"}, } const MAGIC = "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78" func isAlive(host string) bool { conn, err := net.Dial("udp", host) if err != nil { return false } conn.SetReadDeadline(time.Now().Add(5 * time.Second)) pkt := "\x01" + "\x00\x00\x00\x00MERF" + MAGIC conn.Write([]byte(pkt)) resp := make([]byte, 40) rlen, err := conn.Read(resp) if (err != nil) || (rlen == 0) { return false } return true } var wg sync.WaitGroup func waitClose(c chan<- string) { wg.Wait() close(c) } type HostEntry struct { host string owner string } func ping(results chan<- string, e HostEntry) { defer wg.Done() if isAlive(e.host) { results <- fmt.Sprintf("%s is playing at %s", e.owner, e.host) } } func main() { results := make(chan string, 5) for _, host := range hosts { wg.Add(1) go ping(results, host) } go waitClose(results) fmt.Println("Content-type: text/html") fmt.Println("") fmt.Println("") fmt.Println("") fmt.Println("") fmt.Println("") fmt.Println("") fmt.Println("Minecraft PE ping") fmt.Println("") fmt.Println("

Are The Picketts playing Minecraft PE?

") fmt.Println("") if count == 0 { fmt.Println("

Sorry, looks like nobody's playing right now.

") } fmt.Println("") }