我使用这个简单的python服务器代码来发送缓冲区的长度 我将要发送:
def server_mode(ip, port):
try:
s = socket()
s.bind((ip, port))
print ("[*] Listening on %s:%d\n[*] Waiting for clients" % (ip, port))
if ("win32" == os.sys.platform):
print("[*] Press Ctrl + Break to stop server")
elif ("linux" in os.sys.platform):
print("[*] Press Ctrl + C to stop server")
while True:
s.listen(MAX_CLIENTS)
client, client_ip = s.accept()
client_ip = client_ip[0]
print("[*] Got connection from %s" % client_ip)
buf = struct.pack(">I", 7313)
client.send(buf)
client.close()
except KeyboardInterrupt:
s.close()
print("[*] Closed socket")
exit()
except:
s.close()
raise
然后使用这个C代码,客户机应该接收缓冲区的长度 我将发送和malloc缓冲区所需的位置并接收它:
char* buf = malloc(4 * sizeof(char));
recv(sockfd, buf, 4, 0);
int buf_len = *buf;
它可以工作,但只转换4字节长int的前两个字节,
例如,如果我用它来发送大小为13的len
缓冲区,它可以很好地接收,但是对于7313这样的数字,它可以接收54。你知道吗
这是:
不会神奇地从
buf
读取sizeof buf_len
(您似乎期望它是四个)字节,因为*buf
是char
类型的值。你知道吗你应该使用
uint8_t
而不是char
,使用uint32_t
而不是int
,并且根据你的需要阅读,当然要尊重结尾:相关问题 更多 >
编程相关推荐