Skip to content

Instantly share code, notes, and snippets.

@serverhorror
Created June 24, 2012 14:24
Show Gist options
  • Save serverhorror/2983396 to your computer and use it in GitHub Desktop.
Save serverhorror/2983396 to your computer and use it in GitHub Desktop.
Why is there a `@Received(1): "X"` in the output?
package main
// $ go version
//go version go1.0.2
/// OUTPUT (made with echo test | nc -v -w 1 -4 -u 127.0.0.1 1234)
// 2012/06/24 16:22:47 @Received(1): "X"
// 2012/06/24 16:22:47 @Received(5): []byte{0x74, 0x65, 0x73, 0x74, 0xa}
// 2012/06/24 16:22:47 @Received(5): "test"
import (
"flag"
"fmt"
"log"
"net"
"os"
"strings"
)
var (
host = flag.String("host", "localhost:1234", "the port to listen on")
)
func main() {
log.Print("@Booting...")
flag.Parse()
log.Print("@I need to listen here (flag): %s", host)
laddr, err := net.ResolveUDPAddr("udp", *host)
log.Print("@Trying to listen on (UDPAddr): %s", laddr)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
l, err := net.ListenUDP("udp", laddr)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
go func() {
for {
b := make([]byte, 1024)
r, err := l.Read(b)
if err != nil {
log.Printf("@Error: %s -- next round!", err)
continue
} else {
s := fmt.Sprintf("%s", b[:r])
s = strings.TrimSpace(s)
log.Printf("@Received(%d): %#v", r, b[:r])
log.Printf("@Received(%d): %#v", r, s)
}
}
}()
fmt.Scanln()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment