2017-06-02 61 views
-3

我想通過發送一個字節數組(char *)通過一個TCP套接字發送一個結構的數組,然後將其重新轉換爲原始結構類型接收端。套接字接收不工作後memcpy

該結構包含照片的數據,是這樣的:

struct photo{ //List that will store photo information 
    uint32_t id_photo; 
    char *file_name; 
    struct keyword * key_head; 
    struct photo * next; 
}; 

然後我發送這樣的:

photolist p_list[photocount];//Photo count = nº of elements to be sent 
for(int i = 0; i<photocount; i++){ 
    p_list[i]=*aux; 
    p_list[i].key_head = NULL; 
    aux = aux->next; 
} 
//Sending list of photos to the new peer 
buff = malloc(sizeof(photolist)*photocount); 
memcpy(buff, p_list, sizeof(photolist)*photocount); 
nbytes = send(fd, buff, sizeof(photolist)*photocount, 0); 

最後我接收它是這樣的(量的元素的該陣列以前收到sucssefully):

buff = (char *) malloc(sizeof(photolist)*photosize); 
    nbytes = recv(sock_fd_server, buff, sizeof(photolist)*photosize, 0); 
    if(nbytes == -1){ 
     perror("Reciving"); 
     exit(-1); 
    } 
    photolist testlist[photosize]; 
    memcpy(testlist, buff, sizeof(photolist)*photosize); 

但是,當我嘗試打印的EL在接收到結構後,會發生分段錯誤。在發送之前和接收之後,我已經完成了緩衝區的打印,只是爲了查看數據是否相同,並且是這樣,所以我不知道我做錯了什麼。 任何幫助將不勝感激,並提前致謝!

+0

如果我正確閱讀:「有些代碼我沒有顯示你崩潰,下面是一些通信代碼,可能會或可能不會與問題有關」 – John3136

回答

1

您沒有提供photolist 的定義,但如果是從客戶端發送一個結構類似

struct photo{ //List that will store photo information 
    uint32_t id_photo; 
    char *file_name; 
    struct keyword * key_head; 
    struct photo * next; 
}; 

到服務器和服務器訪問file_name爲FILE_NAME包含來自客戶端程序的地址是不是會無法正常工作在服務器程序中有效

+0

哦,當然,多麼愚蠢我的!非常感謝你,對於這樣一個愚蠢的錯誤感到抱歉! – TStriker