我試圖在C中使用TCP套接字編程實現客戶機 - 服務器程序通信。 它位於安裝了Linux操作系統的兩臺64位機器之間。 我想在兩個進程之間傳遞一個c-struct。兩個64位機器之間的TCP套接字編程
爲此,我嘗試使用一個pack - unpack()functioanlity。
請考慮下面的代碼snipt
/*---------------------------------------------------------
on the sending side I have:
---------------------------------------------------------*/
struct packet {
int64_t x;
int64_t y;
int64_t q[maxSize];
} __attribute__((packed));
int main(void)
{
// build packet
struct packet pkt;
pkt.x = htonl(324);
pkt.y = htonl(654);
int i;
for(i = 0; i< maxSize; i++){
pkt.q[i] = i; **// I also try pkt.q[i] = htonl(i);**
}
// and then do the send
}
/*-----------------------------------------------------------------------------
in the receiving side:
-----------------------------------------------------------------------------*/
struct packet {
int64_t x;
int64_t y;
int64_t q[maxSize];
} __attribute__((packed));
static void decodePacket (uint8_t *recv_data, size_t recv_len)
{
// checking size
if (recv_len < sizeof(struct packet)) {
fprintf(stderr, "received too little!");
return;
}
struct packet *recv_packet = (struct packet *)recv_data;
int64_t x = ntohl(recv_packet->x);
int64_t y = ntohl(recv_packet->y);
int i;
printf("Decoded: x=%"PRIu8" y=%"PRIu32"\n", x, y);
for(i=0;i<maxSize;i++){
**//int64_t res = ntohl(recv_packet->q[i]); I also try to print res**
printf("%"PRIu32"\n" , recv_packet->q[i]);
}
}
int main(int argc, char *argv[]){
// receive the data and try to call decodePacket()
int8_t *recv_data = (int8_t *)&buf; //buf is the data received
size_t recv_len = sizeof(buf);
**decode_packet(recv_data, recv_len);**
}
//-----------------------------------------------------------------------------
現在的問題是,我正確地接收到在結構中的x和y的值, 但在結構中的陣問:我收到了一個奇怪的編號,可能的內存碎片值,(我嘗試使用memset()在從另一端接收數據之前用零填充數組,在這種情況下接收全零值)
我不明白爲什麼我沒有收到結構中數組的正確值。
請注意,我嘗試與出htonl(),而投入結構, 而在另一側前填充陣列:以與出再用ntohl(),而陣列從結構解碼
任何幫助將理解的,
您的接收端有幾種不匹配的打印格式 - 這會導致未定義的行爲。修復這些並再試一次? – 2012-08-07 17:14:14
由於問題很可能出現在發送或接收代碼中,因此您忽略了重要的位。一種可能性是該結構需要在兩端聲明爲打包(取決於你如何嘗試發送/接收它)。 – 2012-08-07 17:15:09
是的。而這在編譯器插入不同填充的兩個Linux之間甚至不起作用。你最好連續化數值。甚至不要嘗試直接轉移結構。 – 2012-08-07 17:17:47