我尝试通过 winsockets 在 Python 和 c 程序之间进行进程间通信。发送字符串确实有效,但现在我尝试将一个 int 数组从 c 套接字发送到 python 套接字。
我已经发现我必须使用 htonl() 将 int 数组转换为字节流,因为 winsock2 的 send 函数不能直接发送 int 数组。
现在我想在 python 套接字中使用 ntohl() 但接收函数返回字节,而 ntohl() 需要一个整数值作为输入。
这是我的代码
C端(仅相关部分):
uint32_t a[1] = {1231};
uint32_t a_converted[1]={0};
a_converted[0] = htonl(a[0]);
iResult = send( ConnectSocket, ( char *) a_converted, sizeof( a_converted), 0 );
Python端(只是相关部分):
data = connection.recv(16)
data_i = socket.ntohl(data)