Transfer Image from Raspberry Pi (Python) to Android app (Java)

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon Jul 22 19:20:24 EDT 2019


rkartunova at yahoo.com wrote:
> This
> code does successfully read in the bytes until there are around 2000-3000
> bytes left to be read and then it seems to freeze on the int bytes_read =
> in.read(msg_buff, 0, msg_buff.length) line.

This happens because you're trying to read more bytes than the sender
is sending. The receiver is trying to read a whole bufferful and has no
way to know that there aren't any more bytes coming, so it waits forever.

Your receiver needs to read exactly the right number of bytes. Keep
track of the number of bytes left to read, and when it's less than
the buffer size, issue a read for just that number instead of a whole
bufferful.

(The code you have would work if the sender closed its end of the
connection after sending the image, but you can't do that if you
want to get an OK message back.)

-- 
Greg



More information about the Python-list mailing list