Linux下基于socket多线程并发通信的实现

 
Linux下基于socket多线程并发通信的实现
2016-09-26 19:47:06 /故事大全

Linux下基于socket多线程并发通信的实现

分类:Linux2011-05-21 18:138455人阅读评论(5)收藏举报socket多线程linuxserverstruct服务器[cpp]view plaincopy
  1. /***************************************************
  2. *文件名:pthread_server.c
  3. *文件描述:创建子线程来接收客户端的数据
  4. ***************************************************/
  5. #include<sys/types.h>
  6. #include<sys/socket.h>
  7. #include<stdio.h>
  8. #include<netinet/in.h>
  9. #include<arpa/inet.h>
  10. #include<unistd.h>
  11. #include<stdlib.h>
  12. #include<pthread.h>
  13. void*rec_data(void*fd);
  14. intmain(intargc,char*argv[])
  15. {
  16. intserver_sockfd;
  17. int*client_sockfd;
  18. intserver_len,client_len;
  19. structsockaddr_inserver_address;
  20. structsockaddr_inclient_address;
  21. structsockaddr_intempaddr;
  22. inti,byte;
  23. charchar_recv,char_send;
  24. socklen_ttemplen;
  25. server_sockfd=socket(AF_INET,SOCK_STREAM,0);//创建套接字
  26. server_address.sin_family=AF_INET;
  27. server_address.sin_addr.s_addr=htonl(INADDR_ANY);
  28. server_address.sin_port=htons(9734);
  29. server_len=sizeof(server_address);
  30. bind(server_sockfd,(structsockaddr*)&server_address,server_len);//绑定套接字
  31. templen=sizeof(structsockaddr);
  32. printf("serverwaitingforconnect/n");
  33. while(1){
  34. pthread_tthread;//创建不同的子线程以区别不同的客户端
  35. client_sockfd=(int*)malloc(sizeof(int));
  36. client_len=sizeof(client_address);
  37. *client_sockfd=accept(server_sockfd,(structsockaddr*)&client_address,(socklen_t*)&client_len);
  38. if(-1==*client_sockfd){
  39. perror("accept");
  40. continue;
  41. }
  42. if(pthread_create(&thread,NULL,rec_data,client_sockfd)!=0)//创建子线程
  43. {
  44. perror("pthread_create");
  45. break;
  46. }
  47. }
  48. shutdown(*client_sockfd,2);
  49. shutdown(server_sockfd,2);
  50. }
  51. /*****************************************
  52. *函数名称:rec_data
  53. *功能描述:接受客户端的数据
  54. *参数列表:fd——连接套接字
  55. *返回结果:void
  56. *****************************************/
  57. void*rec_data(void*fd)
  58. {
  59. intclient_sockfd;
  60. inti,byte;
  61. charchar_recv[100];//存放数据
  62. client_sockfd=*((int*)fd);
  63. for(;;)
  64. {
  65. if((byte=recv(client_sockfd,char_recv,100,0))==-1)
  66. {
  67. perror("recv");
  68. exit(EXIT_FAILURE);
  69. }
  70. if(strcmp(char_recv,"exit")==0)//接受到exit时,跳出循环
  71. break;
  72. printf("receivefromclientis%s/n",char_recv);//打印收到的数据
  73. }
  74. free(fd);
  75. close(client_sockfd);
  76. pthread_exit(NULL);
  77. }
  78. /***************************************************
  79. *文件名:pthread_client.c
  80. *文件描述:创建子线程来接收客户端的数据
  81. ***************************************************/
  82. #include<sys/types.h>
  83. #include<sys/socket.h>
  84. #include<stdio.h>
  85. #include<netinet/in.h>
  86. #include<arpa/inet.h>
  87. #include<unistd.h>
  88. #include<stdlib.h>
  89. intmain(intargc,char*argv[])
  90. {
  91. intsockfd;
  92. intlen;
  93. structsockaddr_inaddress;
  94. intresult;
  95. inti,byte;
  96. charchar_send[100]={0};
  97. if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  98. {
  99. perror("socket");
  100. exit(EXIT_FAILURE);
  101. }
  102. if(argc!=3){
  103. printf("Usage:fileclient<address><port>/n");//用法:文件名服务器IP地址服务器端口地址
  104. return0;
  105. }
  106. if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
  107. perror("sock");
  108. exit(1);
  109. }
  110. bzero(&address,sizeof(address));
  111. address.sin_family=AF_INET;
  112. address.sin_port=htons(atoi(argv[2]));
  113. inet_pton(AF_INET,argv[1],&address.sin_addr);
  114. len=sizeof(address);
  115. if((result=connect(sockfd,(structsockaddr*)&address,len))==-1)
  116. {
  117. perror("connect");
  118. exit(EXIT_FAILURE);
  119. }
  120. for(;;)
  121. {
  122. scanf("%s",char_send);//输入发送数据
  123. fflush(stdin);//清除输入缓存
  124. if(strcmp(char_send,"exit")==0){//如果输入exit,跳出循环
  125. if((byte=send(sockfd,char_send,100,0))==-1)
  126. {
  127. perror("send");
  128. exit(EXIT_FAILURE);
  129. }
  130. break;
  131. }
  132. if((byte=send(sockfd,char_send,100,0))==-1)
  133. {
  134. perror("send");
  135. exit(EXIT_FAILURE);
  136. }
  137. }
  138. close(sockfd);
  139. exit(0);
  140. }

编译服务器端程序pthread_server.c:

$gcc pthread_server.c –o server –lpthread

编译客户端程序pthread_client.c:

$gcc pthread_client.c –o client

编译在开发板上跑的客户端程序:

$arm-linux-gcc client.c –o arm_client

先启动服务器端的程序server:

$./server

打开另一个终端,启动客户端的程序client:

$./client 127.0.0.1 9734

把arm_client放到开发板上,启动arm_client:

$./arm_client 219.222.170.9 9734

结果:

服务器端:

tongs@tong"s-desktop:~/c_c++_program/sock_inet_comm2$ ./server

server waiting for connect

receive from client is client

receive from client is client

receive from client is arm_client

receive from client is arm_client

客户端:

tongs@tong"s-desktop:~/c_c++_program/sock_inet_comm2$ ./client 127.0.0.1 9734

client

client

exit

开发板服务器端

[/mnt/yaffs/nfs_share/sock_tcp/thread_socket]./arm_client219.222.170.9 9734

arm_client

arm_client

exit

所属专题:
如果您觉得本文或图片不错,请把它分享给您的朋友吧!

 
搜索
 
 
广告
 
 
广告
 
故事大全
 
版权所有- © 2012-2015 · 故事大全 SITEMAP站点地图-Foton Auman手机看故事 站点地图-Foton Auman