I am trying to send / receive data over unix domain socket between a server written in C and client written in Python. When i try to unpack the data received i am getting this error.
struct.error: unpack str size does not match format
client = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
client.connect("/tmp/udfile")
Msg = struct.pack('I I 64s I 64s I 2048s', 1, 2, "SAMPLE1", 0, "SAMPLE2", 0, "SAMPLE3")
client.sendall(Msg)
Reply = client.recv(2192)
opcode, atype, btype, ctype, dtype, ftype, etype = struct.unpack('I I 64s I 64s I 2048s', Reply)
Would like to know whether this is possible in the first place. If yes, what is that i am missing.
The clue here is SOCK_STREAM
. Stream . You have a stream connection to the other program. Message boundaries are not guaranteed. You got past the first hurdle - you used sendall
instead of send
. You didn't get past the second one, though. recv(N)
is not guaranteed to return N
bytes. It will return up to N
bytes. Since you need exactly 2192 bytes for struct.unpack
to succeed, you need to call recv
in a loop and accumulate the results in a buffer until you have accumulated 2192 bytes. Then you can call struct.unpack
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.