我寫了一個 unsigned char *,兩個 int 并通過 C 中的套接字發送它。我如何從接收方檢索這些值。
代碼:
unsigned char * request = malloc (8 * sizeof(unsigned char));
int index = htonl(rand() % 1000);
int size = htonl(key_size);
memcpy(request, &ind,4);
memcpy(request 4, &size,4);
uj5u.com熱心網友回復:
您只需反轉所有這些操作。從套接字讀取到buffer,然后使用:
int index, size;
memcpy(&index, buffer, 4);
memcpy(&size, buffer 4, 4);
index = ntohl(index);
size = ntohl(size);
請注意,您的所有代碼都假定sizeof int == 4. 最好使用它int32_t來確保變數是預期的大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/518358.html
標籤:C插座未签名
上一篇:fgets給我null
下一篇:無法列印檔案的最后一行
