Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在用C编写的服务器和用python编写的客户端之间通过unix域套接字进行通信_Python_C_Networking_Unix Socket - Fatal编程技术网

在用C编写的服务器和用python编写的客户端之间通过unix域套接字进行通信

在用C编写的服务器和用python编写的客户端之间通过unix域套接字进行通信,python,c,networking,unix-socket,Python,C,Networking,Unix Socket,我试图通过unix域套接字在用C编写的服务器和用Python编写的客户端之间发送/接收数据。当我试图解包收到的数据时,我得到了这个错误 struct.error:解包str大小与格式不匹配 我首先想知道这是否可行。如果是,我遗漏了什么。这里的线索是SOCK\u STREAM。流动您有一个到另一个程序的流连接。不保证消息边界。您通过了第一个障碍-您使用了sendall而不是send。但是你没有通过第二个recv(N)不保证返回N字节。它将返回多达N字节。由于struct.unpack需要2192字

我试图通过unix域套接字在用C编写的服务器和用Python编写的客户端之间发送/接收数据。当我试图解包收到的数据时,我得到了这个错误

struct.error:解包str大小与格式不匹配


我首先想知道这是否可行。如果是,我遗漏了什么。

这里的线索是
SOCK\u STREAM
。流动您有一个到另一个程序的流连接。不保证消息边界。您通过了第一个障碍-您使用了
sendall
而不是
send
。但是你没有通过第二个
recv(N)
不保证返回
N
字节。它将返回多达
N
字节。由于
struct.unpack
需要2192字节才能成功,因此需要在循环中调用
recv
,并将结果累积到缓冲区中,直到累积2192字节。然后可以调用
struct.unpack

错误发生在哪里?收到了什么数据?您是否尝试过使用
pdb
pdb.set_trace()
进行调试?谢谢。我从来不知道这个pdb模块。使用此选项并能够调试问题。尺寸不匹配。
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)