Question

I have a simple script which transfers an image file from one machine to another, does image processing and returns a result as "dice count". The problem is that randomly the image received is partially grey. There seems no reason as to when the transfer is incomplete and I see no other issues in the code. The client gives no error or any indication that sendall failed or was incomplete. Thanks guys.

enter image description here

server code:

def reciveImage():
    #create local buffer file 
    fo = open("C:/foo.jpg", "wb")
    print "inside reciveImage"
    while True:
        print "inside loop"
        data = client.recv(4096)
        print "data length: ", len(data)
        fo.write(data)
        print "data written"
        if (len(data) < 4096):
            break
            print "break"       
    fo.close()
    print "Image received"

And the (simplifyed) client code:

data = open("/home/nao/recordings/cameras/image1.jpg", "rb")    
        # prepare to send data
        binary_data = data.read()
        data.close()

        sock.sendall(binary_data)

Normal server output:

Client Command: findBlob. Requesting image...
inside reciveImage
inside loop
data length:  4096
data written
#... This happens a bunch of times....
inside loop
data length:  4096
data written
inside loop
data length:  1861
data written
Image received
dice count:  0
Waiting for a connection

But randomly it will only loop a few times or less, like:

Client Command: findBlob. Requesting image...
inside reciveImage
inside loop
data length:  1448
data written
Image received
dice count:  0
Waiting for a connection
Was it helpful?

Solution

recv does not block until all data are received, it only blocks until some data are received and returns them. E.g. if the client send first 512 bytes and then a second later another 512 byte your recv will return the first 512 bytes, even if you asked for 4096. So you should only break if recv returns, that no more data are available (connection closed).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top