Linux下基於socket多線程併發通信的實現

Linux下基於socket多線程併發通信的實現

分類: Linux2011-05-21 18:13 8455人閱讀 評論(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. int main(int argc,char *argv[])  

  15. {  

  16.        int server_sockfd;  

  17.     int *client_sockfd;  

  18.        int server_len, client_len;  

  19.        struct sockaddr_in server_address;  

  20.        struct sockaddr_in client_address;  

  21.        struct sockaddr_in tempaddr;  

  22.        int i,byte;  

  23.        char char_recv,char_send;  

  24.        socklen_t templen;  

  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, (struct sockaddr *)&server_address, server_len);//綁定套接字  

  33.        templen = sizeof(struct sockaddr);  

  34.    

  35.        printf("server waiting for connect/n");  

  36.        while(1){  

  37.               pthread_t thread;//創建不同的子線程以區別不同的客戶端  

  38.               client_sockfd = (int *)malloc(sizeof(int));  

  39.               client_len = sizeof(client_address);  

  40.               *client_sockfd = accept(server_sockfd,(struct sockaddr *)&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.        int client_sockfd;  

  63.        int i,byte;  

  64.        char char_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("receive from client is %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. int main(int argc,char *argv[])  

  95. {  

  96.        int sockfd;  

  97.        int len;  

  98.        struct sockaddr_in address;     

  99.        int result;  

  100.        int i,byte;  

  101.        char char_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.       return 0;  

  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, (struct sockaddr *)&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_client  219.222.170.9 9734

arm_client

arm_client

exit

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章