Chinaunix首页 | 论坛 | 博客
  • 博客访问: 649853
  • 博文数量: 150
  • 博客积分: 4070
  • 博客等级: 中校
  • 技术积分: 1795
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-23 21:44
文章分类

全部博文(150)

文章存档

2012年(1)

2011年(123)

2010年(26)

分类:

2011-07-29 16:59:43

1、简介

在一个主程序中,接收客户端的链接,当客户端连接到来时,使用pthread_create函数建立一个线程进程客户端的请求处理,分析数据,给出响应等。

2、tcp模型

image

3、服务器源代码(concurrency-server4.c):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <pthread.h>

  12. #define BUFLEN 1024

  13. /*******************并发服务器模型之四:单客户端单线程,统一accept**********************/

  14. static void *handle_request(void *argv){
  15.     int newfd = *((int *)argv);    
  16.     char buf[BUFLEN];
  17.     int len;
  18.     time_t now;
  19.     /******处理客户端请求*******/
  20.     bzero(buf,BUFLEN);
  21.     len = recv(newfd,buf,BUFLEN,0);
  22.     if(len >0 && !strncmp(buf,"TIME",4)){
  23.         bzero(buf,BUFLEN);
  24.         /*获取系统当前时间*/
  25.         now = time(NULL);
  26.         /*ctime将系统时间转换为字符串,sprintf使转化后的字符串保存在buf*/
  27.         sprintf(buf,"%24s\r\n",ctime(&now));
  28.         //******发送系统时间*******/
  29.         send(newfd,buf,strlen(buf),0);
  30.     }
  31.     /*关闭通讯的套接字*/
  32.     close(newfd);
  33.     return NULL;
  34. }

  35. static void handle_connect(int sockfd){
  36.     int newfd;
  37.     struct sockaddr_in c_addr;
  38.     socklen_t len;
  39.     pthread_t thread_s;

  40.     while(1){
  41.         len = sizeof(struct sockaddr);
  42.         if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) >0){
  43.             printf("\n*****************通信开始***************\n");
  44.             printf("正在与您通信的客户端是:%s: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port));
  45.             /*创建线程来处理连接*/
  46.             pthread_create(&thread_s,NULL,handle_request,(void *)&newfd);            
  47.         }
  48.     }
  49. }

  50. int main(int argc, char **argv)
  51. {
  52.     int sockfd;
  53.     struct sockaddr_in s_addr;
  54.     unsigned int port, listnum;
  55.     
  56.     /*建立socket*/
  57.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
  58.         perror("socket");
  59.         exit(errno);
  60.     }else
  61.         printf("socket create success!\n");
  62.     /*设置服务器端口*/    
  63.     if(argv[2])
  64.         port = atoi(argv[2]);
  65.     else
  66.         port = 4567;
  67.     /*设置侦听队列长度*/
  68.     if(argv[3])
  69.         listnum = atoi(argv[3]);
  70.     else
  71.         listnum = 3;
  72.     /*设置服务器ip*/
  73.     bzero(&s_addr, sizeof(s_addr));
  74.     s_addr.sin_family = AF_INET;
  75.     s_addr.sin_port = htons(port);
  76.     if(argv[1])
  77.         s_addr.sin_addr.s_addr = inet_addr(argv[1]);
  78.     else
  79.         s_addr.sin_addr.s_addr = INADDR_ANY;
  80.     /*把地址和端口帮定到套接字上*/
  81.     if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){
  82.         perror("bind");
  83.         exit(errno);
  84.     }else
  85.         printf("bind success!\n");
  86.     /*侦听本地端口*/
  87.     if(listen(sockfd,listnum) == -1){
  88.         perror("listen");
  89.         exit(errno);    
  90.     }else
  91.         printf("the server is listening!\n");
  92.     /*处理客户端的连接*/
  93.     handle_connect(sockfd);
  94.     /*关闭服务器的套接字*/
  95.     close(sockfd);
  96.     return 0;
  97. }

4、客户端源代码(concurrency-client.c)与之前的一样。

5、编译源代码:

new@new-desktop:~/linux/c$ gcc -Wall –lpthread concurrency-server4.c -o server

new@new-desktop:~/linux/c$ gcc -Wall concurrency-client.c -o client

6、运行,试试吧

阅读(1608) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~