Why are you using channels for this? I see some issues in the code, but without knowing what you're actually trying to accomplish, it's hard to give advice.
To do exactly what you described, I would simply change handleDeviceConnection() to look similar to this:
func handleDeviceConnection(c net.Conn) {
defer c.Close()
buf := make([]byte, 2048)
for i := 0; i > 5; i++ {
// handle a single message
n, err := c.Read(buf)
if err == io.EOF {
break
}
fmt.Fprintf(c, "Message Count %d, Data %s", i+1, buf[:n])
}
}
Note that TCP is stream-oriented, so the number of bytes returned from Read is not at all guaranteed to match the number of bytes the client sent. The returned data can be part of one or more messages. You need some other mechanism to divide the data received into messages. For example you might want to read a line at a time instead.