免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1200 | 回复: 3
打印 上一主题 下一主题

大家看看这段代码有什么问题?关于EPOLL的! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-26 16:49 |只看该作者 |倒序浏览
麻烦大家看一下这段代码,客户端在断开时,服务器端有时能断开,有时不能!基本上是5~6个才能断开一个!

  1. /*---------------------------------------------------------
  2. * TestEpoll.cpp
  3. ---------------------------------------------------------*/

  4. #include <iostream>;
  5. #include <string>;
  6. #include <sys/epoll.h>;
  7. #include <sys/time.h>;
  8. #include <sys/types.h>;
  9. #include <sys/socket.h>;
  10. #include <sys/wait.h>;
  11. #include <netdb.h>;
  12. #include <ctype.h>;
  13. #include <arpa/inet.h>;
  14. #include <fcntl.h>;
  15. #include <signal.h>;
  16. #include <errno.h>;

  17. using namespace std;

  18. #define MAX_CLIENT   10000
  19. #define PORT         8008

  20. int ListenFd = -1;

  21. void nonblock(int sockfd)
  22. {
  23.         int opts;
  24.         opts = fcntl(sockfd, F_GETFL);
  25.         if(opts < 0)
  26.         {
  27.                 cout<<" fcntl(F_GETFL)"<<endl;
  28.                 exit(1);
  29.         }
  30.         opts = (opts | O_NONBLOCK);
  31.         if(fcntl(sockfd, F_SETFL, opts) < 0)
  32.         {
  33.                 cout<<"fcntl(F_SETFL)"<<endl;
  34.                 exit(1);
  35.         }
  36. }

  37. int main()
  38. {
  39.         struct sockaddr_in srv;
  40.         int clifd;

  41.         if((ListenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  42.         {
  43.                 cout<<" Error Create Socket !"<<endl;
  44.                 exit(1);
  45.         }

  46.         int flags, ling;
  47.         setsockopt(ListenFd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
  48.         setsockopt(ListenFd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
  49.         setsockopt(ListenFd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));

  50.         bzero(&srv, sizeof(srv));
  51.         srv.sin_family = AF_INET;
  52.         srv.sin_addr.s_addr = INADDR_ANY;
  53.         srv.sin_port = htons(PORT);

  54.         if(bind(ListenFd, (struct sockaddr*)&srv, sizeof(srv)) < 0)
  55.         {
  56.                 cout<<" Error Bind Port !"<<endl;
  57.                 exit(1);
  58.         }

  59.         listen(ListenFd, 1024);

  60.         int epfd = epoll_create(MAX_CLIENT);
  61.         if(!epfd)
  62.         {
  63.                 cout<<" Error Create Epoll !"<<endl;
  64.                 exit(1);
  65.         }

  66.         struct epoll_event ev;
  67.         ev.events = EPOLLIN | EPOLLET;
  68.         ev.data.fd = ListenFd;

  69.         if(epoll_ctl(epfd, EPOLL_CTL_ADD, ListenFd, &ev) < 0)
  70.         {
  71.                 cout<<" Error Add ListenFd to Epoll !"<<endl;
  72.                 exit(1);
  73.         }

  74.         char buffer[1024];

  75.         for(;;)
  76.         {
  77.                 struct epoll_event *events =  new struct epoll_event;

  78.                 int res = epoll_wait(epfd, events, MAX_CLIENT, -1);

  79.                 for(int i=0; i<res; i++)
  80.                 {
  81.                         if(events[i].data.fd == ListenFd)
  82.                         {
  83.                                 clifd = accept(ListenFd, NULL, NULL);
  84.                                 cout<<"LINK: "<<clifd<<endl;
  85.                                 if(clifd >;= 0)
  86.                                 {
  87.                                         nonblock(clifd);
  88.                                         ev.events = EPOLLIN | EPOLLET;
  89.                                         ev.data.fd = clifd;
  90.                                         if(epoll_ctl(epfd, EPOLL_CTL_ADD, clifd, &ev) < 0)
  91.                                         {
  92.                                                 cout<<" Error EPOLL_CTL_ADD !"<<endl;
  93.                                                 exit(1);
  94.                                         }
  95.                                 }       
  96.                         }          
  97.                         else
  98.                         {
  99.                                 bzero(buffer, strlen(buffer));
  100.                                 int n = recv(events[i].data.fd, buffer, 1024, 0);
  101.                                 if(n == 0)
  102.                                 {
  103.                                         cout<<events[i].data.fd<<" Closeed Connection !"<<endl;
  104.                                         epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
  105.                                         close(events[i].data.fd);
  106.                                 }
  107.                                 else if(n < 0)
  108.                                 {
  109.                                         epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
  110.                                         cout<<events[i].data.fd<<" Error !"<<endl;
  111.                                         close(events[i].data.fd);
  112.                                 }
  113.                                 else
  114.                                 {
  115.                                         cout<<"SOCK: "<<events[i].data.fd<<"RECV: "<<buffer<<endl;
  116.                                         bzero(&buffer, strlen(buffer));
  117.                                         if(send(events[i].data.fd, buffer, strlen(buffer), 0) < 0)
  118.                                                 cout<<"Send Error"<<endl;

  119.                                 }
  120.                         }
  121.                 }
  122.                 delete events;
  123.         }
  124. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-03-26 16:51 |只看该作者

大家看看这段代码有什么问题?关于EPOLL的!

怎么缩进全没了???

论坛徽章:
0
3 [报告]
发表于 2005-03-26 18:36 |只看该作者

大家看看这段代码有什么问题?关于EPOLL的!

[quote]原帖由 "letion"]怎么缩进全没了???[/quote 发表:

楼主把这些全部装在代码按钮弹出的输入框里就行了!

论坛徽章:
0
4 [报告]
发表于 2005-03-26 19:36 |只看该作者

大家看看这段代码有什么问题?关于EPOLL的!

原来如此!多谢提醒!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP