This might has nothing to do with Go.
I've created a simple UDP listener. I'm not sure what it does, but what I want is read data and print them on stdout. Here's the code:
func main() {
service := ":8032"
conn, err := net.ListenPacket("udp", service)
if err != nil { fmt.Println(err) }
for {
var buf [512]byte
n, _ /*addr*/, err := conn.ReadFrom(buf[0:])
fmt.Println("Read bytes:", n)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(buf[0:n]))
}
}
}
When I use another program to send it a lot of data, say 7000 bytes, it reads and prints out 512 bytes as expected (and the other program reports sending 7000 bytes). But first it prints a lot of newlines. This is too many bytes to show nicely in this email, so I'll use a shorter analogy. Imagine:
1. The sending program sends the letters of the alphabet, each on a line.
2. The listening program has a buffer that's big enough for a-h.
3. The listening program prints out a lot of newlines, followed by a-h.
I'm really puzzled by this. Can anyone explain it to me?
Beyond my puzzlement, can someone comment if I'm daft in my general approach? My thought was that if someone sends my listener a huge chunk of data, I'll be able to read it 512 bytes at a time off the connection and reassemble the big chunk. I am not a network expert, so I have some questions I don't know how to answer, such as will there be problems if lots of senders are sending data concurrently, will I miss some of the data if things get too busy, and so forth.
Thanks,
Boris.