-1

I'm using a raspberry as a client and i'm sending a large array to a remote linux server , the problem is when I try with a little message the message is sent , but when i'm sending the array with a 65xxx bytes in length, the server receive nothing. the array btw is pickled.

server.py:

try:

    # Receive our "header" containing message length, it's size is defined and constant
    message_header = client_socket.recv(HEADER_LENGTH)

    # If we received no data, client gracefully closed a connection, for example using socket.close() or socket.shutdown(socket.SHUT_RDWR)
    if not len(message_header):
        return False

    # Convert header to int value
    message_length = int(message_header.decode('utf-8').strip())
    fragments = []
    print(message_length)
    received_len = 0
    while received_len < message_length:


        chunk = client_socket.recv(message_length - received_len)
        if not chunk:
            break
        fragments.append(chunk)
        received_len += len(chunk)

    data_arr = b"".join(fragments)


    # Return an object of message header and message data
    return {'header': message_header, 'data': data_arr}
except:
    return False

client.py:

message = pickle.dumps(faceBlob)

message_header = bytes(f"{len(message):<{HEADER_LENGTH}}", "utf-8")
client_socket.send(message_header + message)
Adil Saidi
  • 181
  • 1
  • 1
  • 10

1 Answers1

0

I wouldn't take a dict. Reserve 8 bit in front of it so that your data packet would be like this

([xxxx xxxx]   [xxxx ...])
([Packet size] [Data])

and then first receive the first 8 bits with recv(8) and then recv(p_len) p_len -> packet length

(you can also reserve 16 bit etc.)

rowBee
  • 61
  • 1
  • 5
  • I understand that I should change the message header to [len(message)] and then send it .. but how do I write the code to receive something and then something else while im sending this as a whole msg – Adil Saidi May 27 '21 at 11:05
  • 1
    I would take IO streams. Then the data can be virtually infinitely long and you can always send pieces between which you can receive again (if I understand that correctly) https://docs.python.org/3/library/io.html – rowBee May 27 '21 at 11:13