• day37:网编day4,多点通信和并发服务器


    一、广播接收方:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define BRD_IP "192.168.114.255"
    7. #define BRD_PORT 8888
    8. int main(int argc, const char *argv[])
    9. {
    10. //创建报式套接字
    11. int sfd = -1;
    12. if((sfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    13. {
    14. ERR_MSG("socket");
    15. }
    16. printf("socket success sfd=%d\n",sfd);
    17. //填充地址信息结构体给bind用
    18. struct sockaddr_in sin;
    19. sin.sin_family = AF_INET; //必须填AF_INET
    20. sin.sin_port = htons(BRD_PORT); //广播端口号
    21. sin.sin_addr.s_addr = inet_addr(BRD_IP); //广播IP
    22. //bind绑定地址信息
    23. if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    24. {
    25. ERR_MSG("bind");
    26. return -1;
    27. }
    28. printf("bind success[%s:%d]\n",BRD_IP,BRD_PORT);
    29. socklen_t addrlen = sizeof(sin);
    30. char buf[128]="";
    31. ssize_t len;
    32. while(1)
    33. {
    34. if((len = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen)) < 0)
    35. {
    36. ERR_MSG("recvfrom");
    37. return -1;
    38. }
    39. printf("recvfrom success %s\n",buf);
    40. }
    41. close(sfd);
    42. return 0;
    43. }

    二、广播发送方:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define BRD_IP "192.168.114.255"
    7. #define BRD_PORT 8888
    8. int main(int argc, const char *argv[])
    9. {
    10. //创建报式套接字
    11. int cfd = -1;
    12. if((cfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    13. {
    14. ERR_MSG("socket");
    15. }
    16. printf("socket success cfd=%d\n",cfd);
    17. int broad = 1;
    18. if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,(int*)&broad,sizeof(broad)) < 0)
    19. {
    20. ERR_MSG("setsockpot");
    21. return-1;
    22. }
    23. printf("setsockpot success\n");
    24. //非必须绑定
    25. //填充地址信息结构体给sendto
    26. struct sockaddr_in sin;
    27. sin.sin_family = AF_INET; //必须填AF_INET
    28. sin.sin_port = htons(BRD_PORT); //广播端口号
    29. sin.sin_addr.s_addr = inet_addr(BRD_IP); //广播IP
    30. printf("bind success[%s:%d]\n",BRD_IP,BRD_PORT);
    31. char buf[128]="";
    32. ssize_t len;
    33. while(1)
    34. {
    35. //清空buf
    36. bzero(buf,sizeof(buf));
    37. //从终端输入数据保存到buf中
    38. fscanf(stdin,"%s",buf);
    39. while(getchar()!=10);
    40. if(sendto(cfd,buf,strlen(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    41. {
    42. ERR_MSG("sendto");
    43. return -1;
    44. }
    45. printf("sendto success\n");
    46. }
    47. close(cfd);
    48. return 0;
    49. }

    三、组播接收方:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define GRP_IP "224.1.2.3" //组播IP
    7. #define IP "192.168.114.94" //本机IP
    8. #define GRP_PORT 8888
    9. int main(int argc, const char *argv[])
    10. {
    11. //创建报式套接字
    12. int sfd = -1;
    13. if((sfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    14. {
    15. ERR_MSG("socket");
    16. }
    17. printf("socket success sfd=%d\n",sfd);
    18. //填充结构体,来完成组播连接
    19. struct ip_mreqn grp;
    20. grp.imr_multiaddr.s_addr = inet_addr(GRP_IP); //组播IP
    21. grp.imr_address.s_addr = inet_addr(IP); //本机IP
    22. grp.imr_ifindex = 0; //自动
    23. //设置连接组播
    24. if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&grp,sizeof(grp)) < 0)
    25. {
    26. ERR_MSG("setsockopt");
    27. return -1;
    28. }
    29. printf("setsockopt success\n");
    30. //填充地址信息结构体给bind用
    31. struct sockaddr_in sin;
    32. sin.sin_family = AF_INET; //必须填AF_INET
    33. sin.sin_port = htons(GRP_PORT); //组播端口号
    34. sin.sin_addr.s_addr = inet_addr(GRP_IP); //组播IP
    35. //bind绑定地址信息
    36. if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    37. {
    38. ERR_MSG("bind");
    39. return -1;
    40. }
    41. printf("bind success[%s:%d]\n",GRP_IP,GRP_PORT);
    42. socklen_t addrlen = sizeof(sin);
    43. char buf[128]="";
    44. ssize_t len;
    45. while(1)
    46. {
    47. if((len = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen)) < 0)
    48. {
    49. ERR_MSG("recvfrom");
    50. return -1;
    51. }
    52. printf("recvfrom success %s\n",buf);
    53. }
    54. close(sfd);
    55. return 0;
    56. }

    四、组播发送方:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define BRD_IP "224.1.2.3"
    7. #define BRD_PORT 8888
    8. int main(int argc, const char *argv[])
    9. {
    10. //创建报式套接字
    11. int cfd = -1;
    12. if((cfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    13. {
    14. ERR_MSG("socket");
    15. }
    16. printf("socket success cfd=%d\n",cfd);
    17. int broad = 1;
    18. if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,(int*)&broad,sizeof(broad)) < 0)
    19. {
    20. ERR_MSG("setsockpot");
    21. return-1;
    22. }
    23. printf("setsockpot success\n");
    24. //非必须绑定
    25. //填充地址信息结构体给sendto
    26. struct sockaddr_in sin;
    27. sin.sin_family = AF_INET; //必须填AF_INET
    28. sin.sin_port = htons(BRD_PORT); //广播端口号
    29. sin.sin_addr.s_addr = inet_addr(BRD_IP); //广播IP
    30. printf("bind success[%s:%d]\n",BRD_IP,BRD_PORT);
    31. char buf[128]="";
    32. ssize_t len;
    33. while(1)
    34. {
    35. //清空buf
    36. bzero(buf,sizeof(buf));
    37. //从终端输入数据保存到buf中
    38. fscanf(stdin,"%s",buf);
    39. while(getchar()!=10);
    40. if(sendto(cfd,buf,strlen(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    41. {
    42. ERR_MSG("sendto");
    43. return -1;
    44. }
    45. printf("sendto success\n");
    46. }
    47. close(cfd);
    48. return 0;
    49. }

    五、TCP多线程并发服务器:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define IP "192.168.114.94"
    7. #define PORT 8888
    8. int del_cli_msg(int newfd,struct sockaddr_in cin);
    9. void handler(int num)
    10. {
    11. if(num == SIGCHLD)
    12. {
    13. while(waitpid(-1,NULL,WNOHANG) > 0);
    14. }
    15. }
    16. int main(int argc, const char *argv[])
    17. {
    18. if(signal(SIGCHLD,handler) == SIG_ERR)
    19. {
    20. ERR_MSG("signal");
    21. return -1;
    22. }
    23. printf("signal success\n");
    24. //创建流式套接字
    25. int sfd = -1;
    26. if((sfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    27. {
    28. ERR_MSG("socket");
    29. return -1;
    30. }
    31. printf("socket success sfd = %d\n",sfd);
    32. //允许端口快速的被复用
    33. int reuse = 1;
    34. if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    35. {
    36. ERR_MSG("setsockopt");
    37. return -1;
    38. }
    39. printf("允许端口快速的被复用成功\n"); //填充地址信息结构体给bind使用
    40. struct sockaddr_in sin;
    41. sin.sin_family = AF_INET;
    42. sin.sin_port = htons(PORT); //服务器端口号
    43. sin.sin_addr.s_addr = inet_addr(IP); //服务器IP
    44. //bind必须绑定IP和端口号
    45. if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    46. {
    47. ERR_MSG("bind");
    48. return -1;
    49. }
    50. printf("bind success [%s:%d]\n",IP,PORT);
    51. //listen把sfd切换成监听模式
    52. if(listen(sfd,128) < 0)
    53. {
    54. ERR_MSG("listen");
    55. return -1;
    56. }
    57. printf("listen success sfd = %d\n",sfd);
    58. struct sockaddr_in cin;
    59. socklen_t addrlen=sizeof(cin);
    60. int newfd =-1;
    61. pid_t pid;
    62. while(1)
    63. {
    64. //accept接收客户端发送过来的地址信息,创建一个newfd与其通讯accept
    65. //父进程专门负责连接
    66. if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) < 0)
    67. {
    68. ERR_MSG("accept");
    69. return -1;
    70. }
    71. printf("[%s:%d]客户端连接成功 newfd = %d\n",\
    72. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
    73. pid = fork();
    74. if(0 == pid)
    75. {
    76. close(sfd);
    77. //子进程专门负责交互
    78. del_cli_msg(newfd,sin);
    79. exit(0);
    80. }
    81. close(newfd);
    82. }
    83. close(sfd);
    84. return 0;
    85. }
    86. int del_cli_msg(int newfd,struct sockaddr_in cin)
    87. {
    88. char buf[128] ="";
    89. ssize_t len = 0;
    90. while(1)
    91. {
    92. bzero(buf,sizeof(buf));
    93. len = recv(newfd,buf,sizeof(buf),0);
    94. if(len< 0)
    95. {
    96. ERR_MSG("recvfrom");
    97. return -1;
    98. }
    99. else if(len == 0)
    100. {
    101. printf("[%s:%d]客户端已下线 newfd = %d\n",\
    102. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
    103. break;
    104. }
    105. printf("recv success [%s:%d],newfd=%d buf=%s\n",\
    106. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
    107. /*
    108. bzero(buf,sizeof(buf));
    109. printf("请输入数据发送给客户端");
    110. fscanf(stdin,"%s",buf);
    111. while(getchar()!=10);
    112. */
    113. strcat(buf,">-<");
    114. if(send(newfd,buf,strlen(buf),0) < 0)
    115. {
    116. ERR_MSG("send");
    117. return -1;
    118. }
    119. printf("send success buf = %s\n",buf);
    120. }
    121. }

    六、TCP多线程并发服务器:

    1. #include <myhead.h>
    2. #define ERR_MSG(msg) do{\
    3. fprintf(stderr,"__%d__\n",__LINE__);\
    4. perror(msg);\
    5. }while(0)
    6. #define IP "192.168.114.94"
    7. #define PORT 8888
    8. struct Data
    9. {
    10. int newfd;
    11. struct sockaddr_in cin;
    12. };
    13. void *del_cli_msg(void *arg);
    14. int main(int argc, const char *argv[])
    15. {
    16. printf("signal success\n");
    17. //创建流式套接字
    18. int sfd = -1;
    19. if((sfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    20. {
    21. ERR_MSG("socket");
    22. return -1;
    23. }
    24. printf("socket success sfd = %d\n",sfd);
    25. //允许端口快速的被复用
    26. int reuse = 1;
    27. if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    28. {
    29. ERR_MSG("setsockopt");
    30. return -1;
    31. }
    32. printf("允许端口快速的被复用成功\n"); //填充地址信息结构体给bind使用
    33. struct sockaddr_in sin;
    34. sin.sin_family = AF_INET;
    35. sin.sin_port = htons(PORT); //服务器端口号
    36. sin.sin_addr.s_addr = inet_addr(IP); //服务器IP
    37. //bind必须绑定IP和端口号
    38. if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    39. {
    40. ERR_MSG("bind");
    41. return -1;
    42. }
    43. printf("bind success [%s:%d]\n",IP,PORT);
    44. //listen把sfd切换成监听模式
    45. if(listen(sfd,128) < 0)
    46. {
    47. ERR_MSG("listen");
    48. return -1;
    49. }
    50. printf("listen success sfd = %d\n",sfd);
    51. struct sockaddr_in cin;
    52. socklen_t addrlen=sizeof(cin);
    53. int newfd =-1;
    54. pthread_t tid;
    55. while(1)
    56. {
    57. //accept接收客户端发送过来的地址信息,创建一个newfd与其通讯accept
    58. //父进程专门负责连接
    59. if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) < 0)
    60. {
    61. ERR_MSG("accept");
    62. return -1;
    63. }
    64. printf("[%s:%d]客户端连接成功 newfd = %d\n",\
    65. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
    66. struct Data data;
    67. data.newfd = newfd;
    68. data.cin = cin;
    69. if(pthread_create(&tid,NULL,del_cli_msg,&data))
    70. {
    71. printf("pthread_create error\n");
    72. return -1;
    73. }
    74. printf("create success\n");
    75. pthread_detach(tid);
    76. }
    77. close(sfd);
    78. return 0;
    79. }
    80. void *del_cli_msg(void *arg)
    81. {
    82. int newfd = ((struct Data*)arg)->newfd;
    83. struct sockaddr_in cin = ((struct Data*)arg)->cin;
    84. char buf[128] ="";
    85. ssize_t len = 0;
    86. while(1)
    87. {
    88. bzero(buf,sizeof(buf));
    89. len = recv(newfd,buf,sizeof(buf),0);
    90. if(len< 0)
    91. {
    92. ERR_MSG("recvfrom");
    93. break;
    94. }
    95. else if(len == 0)
    96. {
    97. printf("[%s:%d]客户端已下线 newfd = %d\n",\
    98. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
    99. break;
    100. }
    101. printf("recv success [%s:%d],newfd=%d buf=%s\n",\
    102. inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
    103. /*
    104. bzero(buf,sizeof(buf));
    105. printf("请输入数据发送给客户端");
    106. fscanf(stdin,"%s",buf);
    107. while(getchar()!=10);
    108. */
    109. strcat(buf,">-<");
    110. if(send(newfd,buf,strlen(buf),0) < 0)
    111. {
    112. ERR_MSG("send");
    113. break;
    114. }
    115. //printf("send success buf = %s\n",buf);
    116. }
    117. }

    七、多线程并发服务器为什么不能将newfd定义成全局?

    不能,因为如果newfd设成全局变量,而多线程之间共享0-3G用户空间,临界资源,所以会导致newfd变化后,原先线程进行交互。

    八、基于UDP的TFTP文件传输,完成下载和上传功能,制作的是客户端,与tftp服务器进行交互

    1. #include <myhead.h>
    2. #define ERR(s) do\
    3. {\
    4. fprintf(stderr,"__%d__",__LINE__);\
    5. perror(s);\
    6. }while(0)
    7. #define PORT 69
    8. #define IP "192.168.114.103"
    9. int do_download(int cfd,struct sockaddr_in sin);
    10. int do_upload(int cfd,struct sockaddr_in sin);
    11. int main(int argc, const char *argv[])
    12. {
    13. //创建报式套接字socket
    14. int cfd;
    15. if((cfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    16. {
    17. ERR("socket");
    18. return -1;
    19. }
    20. printf("socket success\n");
    21. struct sockaddr_in sin;
    22. sin.sin_family = AF_INET;
    23. sin.sin_port = htons(PORT);
    24. sin.sin_addr.s_addr = inet_addr(IP);
    25. char choose = 0;
    26. while(1)
    27. {
    28. system("clear");
    29. printf("--------------------------------\n");
    30. printf("-------------1下载--------------\n");
    31. printf("-------------2上传--------------\n");
    32. printf("-------------3退出--------------\n");
    33. printf("--------------------------------\n");
    34. printf("请输入>>>");
    35. scanf("%c",&choose);
    36. while(getchar()!=10); //吸收垃圾字符
    37. switch(choose)
    38. {
    39. case '1':
    40. do_download(cfd,sin);
    41. break;
    42. case '2':
    43. do_upload(cfd,sin);
    44. break;
    45. case '3':
    46. goto END;
    47. break;
    48. default :
    49. printf("输入错误,请重新输入\n");
    50. }
    51. printf("请输入任意字符清屏\n");
    52. while(getchar()!=10);
    53. }
    54. END:
    55. //关闭套接字
    56. close(cfd);
    57. return 0;
    58. }
    59. int do_download(int cfd,struct sockaddr_in sin)
    60. {
    61. /*
    62. //组下载请求包
    63. char buf[516] = "";
    64. char f_n[20] = "";
    65. //操作码
    66. unsigned short *ptr1 = (unsigned short*)buf;
    67. *ptr1 = htons(1); //组操作码
    68. //文件名
    69. char *ptr2 = (char *)(ptr1+1);
    70. strcpy(ptr2,f_n);
    71. //模式
    72. char *ptr3 = ptr2 + strlen(f_n) + 1;
    73. strcpy(ptr3,"octet");
    74. int size = 2+strlen(ptr2)+1+strlen(ptr3)+1;
    75. */
    76. char buf[516] = "";
    77. char f_n[20] = "";
    78. int num=1;
    79. printf("请输入文件名>>> ");
    80. //终端输入要下载的文件名
    81. fgets(f_n,sizeof(f_n),stdin);
    82. f_n[strlen(f_n)-1] = '\0';
    83. int fd;
    84. if((fd = open(f_n,O_RDONLY|O_CREAT|O_TRUNC,0664)) == -1)
    85. {
    86. ERR("open");
    87. return -1;
    88. }
    89. //用sprintf拼接
    90. int size = sprintf(buf,"%c%c%s%c%s%c",0,1,f_n,0,"octet",0);
    91. //发送下载请求sendto
    92. if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
    93. {
    94. ERR("sendto askpackage");
    95. return -1;
    96. }
    97. printf("sendto askpackage success\n");
    98. ssize_t len = -1;
    99. struct sockaddr_in addrin;
    100. socklen_t addrlen=sizeof(addrin);
    101. while(1)
    102. {
    103. //清空buf
    104. bzero(buf,sizeof(buf));
    105. //接收数据recvfrom,接收地址信息
    106. if((len = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&addrin,&addrlen)) < 0)
    107. {
    108. ERR("recvfrom datapackage");
    109. return -1;
    110. }
    111. unsigned short ptr2=ntohs(*(unsigned short*)(buf+2));
    112. printf("%hu\n",ptr2);
    113. //当操作码为5
    114. if(5 == buf[1])
    115. {
    116. fprintf(stderr, "错误码:%d,错误信息:%s\n", ntohs(*(unsigned short*)(buf+2)), buf+4);
    117. break;
    118. }
    119. //当操作码为3,并且块编号正确时
    120. else if(3 == buf[1] && num == ptr2)
    121. {
    122. printf("%d\n",fd);
    123. //跳过前四个字节,写入文件
    124. if(write(fd,buf+4,len-4)<0)
    125. {
    126. ERR("write");
    127. return -1;
    128. }
    129. //组ACK包
    130. buf[1] = 4;
    131. //发送ACK包,sendto
    132. if(sendto(cfd,buf,4,0,(struct sockaddr*)&addrin,addrlen) < 0)
    133. {
    134. ERR("sendto ACKpackage");
    135. return -1;
    136. }
    137. num++;
    138. //判断数据是否小于512个字节,若小于则下载完成
    139. if(len < 516)
    140. {
    141. printf("download success\n");
    142. break;
    143. }
    144. }
    145. }
    146. close(fd);
    147. return 0;
    148. }
    149. int do_upload(int cfd,struct sockaddr_in sin)
    150. {
    151. char f_n[20]="";
    152. printf("请输入传输文件名>>>");
    153. fscanf(stdin,"%s",f_n);
    154. while(getchar()!=10);
    155. //打开文件
    156. int srcfd = -1;
    157. if((srcfd = open(f_n,O_RDONLY)) < 0)
    158. {
    159. if(errno == ENOENT)
    160. {
    161. printf("文件不存在,请重新输入>>>");
    162. return -2;
    163. }
    164. else
    165. {
    166. ERR("open srcfile");
    167. return -1;
    168. }
    169. }
    170. char buf[516]="";
    171. //用sprintf拼接读写请求封包
    172. int size=sprintf(buf,"%c%c%s%c%s%c",0,2,f_n,0,"octet",0);
    173. if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    174. {
    175. ERR("sendto askpackage");
    176. return -1;
    177. }
    178. printf("sendto askpackage success\n");
    179. //记录块编号
    180. int num=0;
    181. unsigned short* ptr = (unsigned short *)(buf+2);
    182. //记录我要传过去数据的大小
    183. ssize_t len = -1;
    184. socklen_t addrlen = sizeof(sin);
    185. while(1)
    186. {
    187. bzero(buf,sizeof(buf));
    188. //接受ACK
    189. if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen) < 0)
    190. {
    191. ERR("recvfrom ACKpackage");
    192. return -1;
    193. }
    194. printf("recvfrom ACKpackage success\n");
    195. //当传过来的是ACK并且对应的块编号是需要的块编号时
    196. if(buf[1] == 4)
    197. {
    198. if(*ptr == htons(num))
    199. {
    200. num++;
    201. //把操作码改为3,向服务器发送数据
    202. buf[1] = 3;
    203. //*(unsigned short *)(buf+2)= htons(num);
    204. *ptr = htons(num);
    205. //从文件中读取数据,保存到buf中
    206. len = read(srcfd,buf+4,sizeof(buf)-4);
    207. if(len < 0)
    208. {
    209. ERR("read srcfd");
    210. return -1;
    211. }
    212. else if(0 == len)
    213. {
    214. printf("文件上传完毕\n");
    215. break;
    216. }
    217. //向服务器发送打包好的数据包
    218. if(sendto(cfd,buf,len+4,0,(struct sockaddr*)&sin,addrlen) < 0)
    219. {
    220. ERR("sendto datapackage");
    221. return -1;
    222. }
    223. printf("sendto datapackage success\n");
    224. }else{
    225. printf("网络不好,上传数据失败\n");
    226. break;
    227. }
    228. }
    229. else if(buf[1] == 5)
    230. {
    231. fprintf(stderr,"错误码:%d,错误信息:%s\n",\
    232. ntohs(*(unsigned short*)(buf+2)),buf+4);
    233. break;
    234. }
    235. }
    236. return 0;
    237. }

  • 相关阅读:
    5G专网技术研究及其行业应用
    Spark计算框架
    Fabric.js 喷雾笔刷 从入门到放肆
    css知识学习系列(12)-每天10个知识点
    Linux》权限初识
    数字化会员留存怎么做?会员积分体系如何设计?
    c++ 类的多态以及虚函数
    java计算机毕业设计特色农产品供需销售系统源程序+mysql+系统+lw文档+远程调试
    GFS分布式文件系统
    什么是FSDM的九大数据概念
  • 原文地址:https://blog.csdn.net/wxmchong/article/details/132590889