I am trying to send a buffer over unix domain socket to an application , receive the same structure back with updated values and then send that buffer back to that application. I am able to send a newly packed data and receive the response back, but if i try to pack the received buffer and send it again over the socket, i am facing errors saying there is a mismatch in the size which is sent to the application which is listening over the socket and it closes the socket.
The below snip is what i am trying to achieve. Looks like the endianess / string conversion I am depending on to send the data back is not correct.
""" request struct
#structure i am sending over unix domain socket
struct prod_entry {
unsigned int Model;
unsigned int year;
char prodname[64];
}
"""
value = (1, 1992, "mustang")
What is that i am doing wrong here. I want to receive a buffer pack it and send it again.
prod_entry = struct.pack('I I 64s', *value)
def update(update_records):
try:
comm_sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
except socket.error:
return
try:
comm_sock.connect(PROD_UNIX_DOMAIN_SKT)
except socket.gaierror:
return
try:
comm_sock.sendall(update_records)
except socket.error:
return
reply = comm_sock.recv(struct.calcsize('I I 64s '))
out1 = struct.unpack('<I I 64s',reply)
rebound = struct.pack('I I 64s', *out1)
comm_sock.sendall(rebound)
reply2 = comm_sock.recv(struct.calcsize('I I 64s '))
out2 = struct.unpack('<I I 64s',reply2)
comm_sock.close()
update(prod_entry)
I am getting ::struct.error: unpack str size does not match format
The recv
bufsize
argument only specifies a maximum size. recv
may return a smaller chunk than the maximum specified size. You need to loop over recv
in case it's not returning the full packed string.
You'll quickly find that you need a protocol for any network communication like this. It might be useful for you to familiarize yourself with (eg) the HTTP protocol, esp. the request methods .
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.