client端两个函数已经完成,其中上传测试成功(仅client部分),下载未测试
1 #include <stdio.h> 2 #include <string.h> 3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <arpa/inet.h> 6 #include "message.h" 7 8 int AddNode(int sockfd,const Node *node_ptr,int client_num) 9 { 10 Message upd; 11 upd.op=0;//0 means update 12 upd.client_num=client_num; 13 memcpy(&upd.node,node_ptr,sizeof(Node));//copy the node to the message packaage 14 15 int n=send(sockfd,&upd,sizeof(Message),MSG_WAITALL); 16 if(n<0) 17 return 1; 18 return 0; 19 } 20 21 22 int GetNode(int sockfd,Node *node_ptr,int client_num) 23 { 24 Message fet; 25 fet.op=1;//1 means get info from server 26 fet.client_num=client_num; 27 memcpy(&fet.node,node_ptr,sizeof(Node)); 28 29 int n=send(sockfd,&fet,sizeof(Message),MSG_WAITALL); 30 if(n<0) 31 { 32 printf("Send Message Error!\n"); 33 return 1; 34 } 35 36 n=recv(sockfd,&fet,sizeof(Message),MSG_WAITALL); 37 if(n<0||fet.client_num!=client_num) 38 { 39 printf("Wrong Message!\n"); 40 return 1; 41 } 42 43 memcpy(node_ptr,&fet.node,sizeof(Node)); 44 45 return 0; 46 }
项目报告140303(补):client,布布扣,bubuko.com
原文:http://www.cnblogs.com/keepcalmandcarryon/p/3584974.html