diff --git a/src/tcp/tcp.go b/src/tcp/tcp.go index ba21f74..4177f77 100644 --- a/src/tcp/tcp.go +++ b/src/tcp/tcp.go @@ -126,15 +126,19 @@ func chanFromConn(conn net.Conn) chan []byte { c := make(chan []byte) go func() { + b := make([]byte, models.TCP_BUFFER_SIZE) for { - b := make([]byte, models.TCP_BUFFER_SIZE) - _, err := conn.Read(b) + n, err := conn.Read(b) + if n > 0 { + res := make([]byte, n) + // Copy the buffer so it doesn't get changed while read by the recipient. + copy(res, b[:n]) + c <- res + } if err != nil { c <- nil break - } else { - c <- b } } }()