位置:首页 > 应用软件 > Linux下基于socket多线程并发通信的实现

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

时间:2014-09-01  |  作者:网络  |  阅读:205

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

编译服务器端程序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

福利游戏

相关文章

更多

精选合集

更多

大家都在玩

热门话题

大家都在看

更多