Chinaunix首页 | 论坛 | 博客
  • 博客访问: 713303
  • 博文数量: 102
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1748
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-23 15:42
个人简介

寻找严肃、沉默和专注的力量。

文章分类

全部博文(102)

文章存档

2015年(26)

2014年(8)

2013年(68)

分类: C/C++

2013-07-08 19:59:12

local.h

点击(此处)折叠或打开

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

  13. #define BUF_SIZE 1024 //默认缓冲区
  14. #define SERVER_PORT 8888 //监听端口
  15. //#define SERVER_HOST "192.168.34.15" //服务器IP地址
  16. #define SERVER_HOST "127.0.0.1" //服务器IP地址
  17. #define EPOLL_RUN_TIMEOUT -1 //epoll的超时时间
  18. #define EPOLL_SIZE 10000 //epoll监听的客户端的最大数目

  19. #define STR_WELCOME "Welcome to seChat! You ID is: Client #%d"
  20. #define STR_MESSAGE "Client #%d>> %s"
  21. #define STR_NOONE_CONNECTED "Noone connected to server except you!"
  22. #define CMD_EXIT "EXIT"

  23. //两个有用的宏定义:检查和赋值并且检测
  24. #define CHK(eval) if(eval < 0){perror("eval"); exit(-1);}
  25. #define CHK2(res, eval) if((res = eval) < 0){perror("eval"); exit(-1);}

  26. //================================================================================================
  27. //函数名: setnonblocking
  28. //函数描述: 设置socket为不阻塞
  29. //输入: [in] sockfd socket标示符
  30. //输出: 无
  31. //返回: 0
  32. //================================================================================================
  33. int setnonblocking(int sockfd);

  34. //================================================================================================
  35. //函数名: handle_message
  36. //函数描述: 处理每个客户端socket
  37. //输入: [in] new_fd socket标示符
  38. //输出: 无
  39. //返回: 返回从客户端接受的数据的长度
  40. //================================================================================================
  41. int handle_message(int new_fd);

utils.h

点击(此处)折叠或打开

  1. #include "local.h"


  2. int setnonblocking(int sockfd)
  3. {
  4.     CHK(fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK));
  5.     return 0;
  6. }

server.cpp

点击(此处)折叠或打开

  1. #include "local.h"
  2. #include "utils.h"

  3. using namespace std;

  4. // 存放客户端socket描述符的list
  5. list<int> clients_list;

  6. int main(int argc, char *argv[])
  7. {
  8.     int listener; //监听socket
  9.     struct sockaddr_in addr, their_addr;
  10.     addr.sin_family = PF_INET;
  11.     addr.sin_port = htons(SERVER_PORT);
  12.     addr.sin_addr.s_addr = inet_addr(SERVER_HOST);
  13.     socklen_t socklen;
  14.     socklen = sizeof(struct sockaddr_in);

  15.     static struct epoll_event ev, events[EPOLL_SIZE];
  16.     ev.events = EPOLLIN | EPOLLET; //对读感兴趣,边沿触发

  17.     char message[BUF_SIZE];

  18.     int epfd; //epoll描述符
  19.     clock_t tStart; //计算程序运行时间

  20.     int client, res, epoll_events_count;

  21.     CHK2(listener, socket(PF_INET, SOCK_STREAM, 0)); //初始化监听socket
  22.     setnonblocking(listener); //设置监听socket为不阻塞
  23.     CHK(bind(listener, (struct sockaddr *)&addr, sizeof(addr))); //绑定监听socket
  24.     CHK(listen(listener, 1)); //设置监听

  25.     CHK2(epfd,epoll_create(EPOLL_SIZE)); //创建一个epoll描述符,并将监听socket加入epoll
  26.     ev.data.fd = listener;
  27.     CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, listener, &ev));

  28.     while(1)
  29.     {
  30.         CHK2(epoll_events_count,epoll_wait(epfd, events, EPOLL_SIZE, EPOLL_RUN_TIMEOUT));
  31.         tStart = clock();
  32.         for(int i = 0; i < epoll_events_count ; i++)
  33.         {
  34.             if(events[i].data.fd == listener) //新的连接到来,将连接添加到epoll中,并发送欢迎消息
  35.             {
  36.                 CHK2(client,accept(listener, (struct sockaddr *) &their_addr, &socklen));
  37.                 setnonblocking(client);
  38.                 ev.data.fd = client;
  39.                 CHK(epoll_ctl(epfd, EPOLL_CTL_ADD, client, &ev));

  40.                 clients_list.push_back(client); // 添加新的客户端到list
  41.                 bzero(message, BUF_SIZE);
  42.                 res = sprintf(message, STR_WELCOME, client);
  43.                 CHK2(res, send(client, message, BUF_SIZE, 0));

  44.             }else
  45.             {
  46.                 CHK2(res,handle_message(events[i].data.fd)); //注意:这里并没有调用epoll_ctl重新设置socket的事件类型,但还是可以继续收到客户端发送过来的信息
  47.             }
  48.         }
  49.         printf("Statistics: %d events handled at: %.2f second(s)\n", epoll_events_count, (double)(clock() - tStart)/CLOCKS_PER_SEC);
  50.     }

  51.     close(listener);
  52.     close(epfd);

  53.     return 0;
  54. }

  55. int handle_message(int client)
  56. {
  57.     char buf[BUF_SIZE], message[BUF_SIZE];
  58.     bzero(buf, BUF_SIZE);
  59.     bzero(message, BUF_SIZE);

  60.     int len;

  61.     CHK2(len,recv(client, buf, BUF_SIZE, 0)); //接受客户端信息

  62.     if(len == 0) //客户端关闭或出错,关闭socket,并从list移除socket
  63.     {
  64.         CHK(close(client));
  65.         clients_list.remove(client);
  66.     }
  67.     else //向客户端发送信息
  68.     {
  69.         if(clients_list.size() == 1)
  70.         {
  71.             CHK(send(client, STR_NOONE_CONNECTED, strlen(STR_NOONE_CONNECTED), 0));
  72.                 return len;
  73.         }
  74.         
  75.         sprintf(message, STR_MESSAGE, client, buf);
  76.         list<int>::iterator it;
  77.         for(it = clients_list.begin(); it != clients_list.end(); it++)
  78.         {
  79.            if(*it != client)
  80.            {
  81.                 CHK(send(*it, message, BUF_SIZE, 0));
  82.            }
  83.         }
  84.     }

  85.     return len;
  86. }


10kclient.cpp

点击(此处)折叠或打开

  1. #include "local.h"
  2. #include "utils.h"

  3. using namespace std;

  4. char message[BUF_SIZE]; //接受服务器信息
  5. list<int> list_of_clients; //存放所有客户端
  6. int res;
  7. clock_t tStart;

  8. int main(int argc, char *argv[])
  9. {
  10.     int sock;
  11.     struct sockaddr_in addr;
  12.     addr.sin_family = PF_INET;
  13.     addr.sin_port = htons(SERVER_PORT);
  14.     addr.sin_addr.s_addr = inet_addr(SERVER_HOST);
  15.     
  16.     tStart = clock();

  17.     for(int i=0 ; i<EPOLL_SIZE; i++) //生成EPOLL_SIZE个客户端,这里是10000个,模拟高并发
  18.     {
  19.        CHK2(sock,socket(PF_INET, SOCK_STREAM, 0));
  20.        CHK(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0);
  21.        list_of_clients.push_back(sock);

  22.        bzero(&message, BUF_SIZE);
  23.        CHK2(res,recv(sock, message, BUF_SIZE, 0));
  24.        printf("%s\n", message);
  25.     }
  26.    
  27.     list<int>::iterator it; //移除所有客户端
  28.     for(it = list_of_clients.begin(); it != list_of_clients.end() ; it++)
  29.        close(*it);

  30.     printf("Test passed at: %.2f second(s)\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  31.     printf("Total server connections was: %d\n", EPOLL_SIZE);
  32.     
  33.     return 0;
  34. }


参考资料:
google code svn路径:
http://www.cnblogs.com/venow/archive/2012/11/30/2790031.html
阅读(5398) | 评论(2) | 转发(1) |
0

上一篇:C10K问题

下一篇:auto_ptr浅析

给主人留下些什么吧!~~