http://blog.csdn.net/chenzhoutong/article/details/6436807 

分类:  Linux2011-05-21 18:13  8455人阅读  评论(5)  收藏  举报
[cpp]  view plain copy
  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


Logo

更多推荐