I’ve got two small programs communicating nicely over a socket where the receiving side is in Go. Everything works peachy when my messages are tiny enough to fit in the 1024 byte buffer and can be received in a single Read from the connection but now I want to transfer data from an image that is 100k+ or more. I’m assuming the correct solution is not to increase the buffer until any image can fit inside.
Pseudo-go:
var buf = make([]byte,1024)
conn, err := net.Dial("tcp", ":1234")
for {
r, err := conn.Read(buf[0:])
go readHandler(string(buf[0:r]),conn)
}
How can I improve my socket read routine to accept both simple messages of a few bytes and also larger data? Bonus points if you can turn the total image data into an io.Reader for use in image.Decode.
You can use
io.ReadFullto read a[]byteof a specific length. This assumes that you know beforehand how many bytes you need to read.As for
image.Decode, it should be possible to pass theconndirectly to theimage.Decodefunction. This assumes that you do not perform any reads from the connection until the image is decoded.Your code
seems to be suggesting that the goroutine you are starting is reading from
connThis doesn’t seem like a good idea, because you will end up having multiple concurrent reads from the connection (without having control over the order in which the reads will happen): one in the for-loop, another one inreadHandler.