Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6660550
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: C/C++

2015-12-04 18:12:05



I am trying to implement a socket server by using epoll. I have 2 threads doing 2 tasks:

  1. listening to incoming connection
  2. writing on screen the data the client is sending.

For my test I have the client and the server on the same machine with 3 or 4 clients running. The server works fine until I don't kill one of the client by issuing a CTRL-C: as soon I do that the server starts looping and printing at a very fast rate data from other client. The strange thing is that

  1. the client sends data each 2 seconds but the rate of the server is higher
  2. epoll_wait is also supposed to print something when one of the client disconnects as it is checking also for EPOLLHUP or EPOLLERR
  3. epoll_wait should wait a bit before printing since I gave him a timeout of 3000 milliseconds.

Can you help? Could it be that I am passing in a wrong way the epoll descriptor to the other thread? I cannot understand since the code looks similar to many examples around.

Thanks a lot

Mn


server.cpp

点击(此处)折叠或打开

  1. // server.cpp
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. extern "C" {
  6. #include <sys/epoll.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <netdb.h>
  10. #include <pthread.h>
  11. #include <unistd.h>
  12. }
  13. #define MAX_BACKLOG 10
  14. void *readerthread(void *args)
  15. {
  16.     int epfd = *((int *)args);
  17.     epoll_event outwait[10];
  18.     while (true) {
  19.         int retpw = epoll_wait(epfd, outwait, 20, 3000);
  20.         if (retpw == -1) {
  21.             printf("epoll error %m\n");
  22.         } else if (retpw == 0) {
  23.             printf("nothing is ready yet\n");
  24.             continue;
  25.         } else {
  26.             for (int i = 0; i < retpw; i++) {
  27.                 if (outwait[i].events & EPOLLIN) {
  28.                     int fd = outwait[i].data.fd;
  29.                     char buf[64];
  30.                     if (-1 == read(fd, buf, 64)) {
  31.                         printf("error reading %m\n");
  32.                     }
  33.                     printf("%s\n", buf);
  34.                 } else {
  35.                     std::cout << "other event" << std::endl;
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }

  41. int main()
  42. {

  43.     int epfd = epoll_create(10);
  44.     if (-1 == epfd) {
  45.         std::cerr << "error creating EPOLL server" << std::endl;
  46.         return -1;
  47.     }

  48.     pthread_t reader;
  49.     int rt = pthread_create(&reader, NULL, readerthread, (void *)&epfd);
  50.     if (-1 == rt) {
  51.         printf("thread creation %m\n");
  52.         return -1;
  53.     }

  54.     struct addrinfo addr;
  55.     memset(&addr, 0, sizeof(addrinfo));
  56.     addr.ai_family = AF_INET;
  57.     addr.ai_socktype = SOCK_STREAM;
  58.     addr.ai_protocol = 0;
  59.     addr.ai_flags = AI_PASSIVE;

  60.     struct addrinfo *rp, *result;
  61.     getaddrinfo("localhost", "59000", &addr, &result);
  62.     for (rp = result; rp != NULL; rp = rp->ai_next) {

  63.         // we want to take the first ( it could be IP_V4
  64.         // or IP_V6 )
  65.         break;
  66.     }

  67.     int sd = socket(AF_INET, SOCK_STREAM, 0);
  68.     if (-1 == sd) {
  69.         std::cerr << "error creating the socket" << std::endl;
  70.         return -1;
  71.     }
  72.     // to avoid error 'Address already in Use'
  73.     int optval = 1;
  74.     setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

  75.     if (-1 == bind(sd, result->ai_addr, result->ai_addrlen)) {
  76.         printf("%m\n");
  77.         std::cerr << "error binding" << std::endl;
  78.         return -1;
  79.     }

  80.     while (true) {

  81.         std::cout << "listen" << std::endl;
  82.         if (-1 == listen(sd, MAX_BACKLOG)) {
  83.             std::cerr << "listen didn't work" << std::endl;
  84.             return -1;
  85.         }

  86.         std::cout << "accept" << std::endl;
  87.         sockaddr peer;
  88.         socklen_t addr_size;
  89.         int pfd = accept(sd, &peer, &addr_size);
  90.         if (pfd == -1) {
  91.             std::cerr << "error calling accept()" << std::endl;
  92.             return -1;
  93.         }
  94.         epoll_event ev;
  95.         ev.data.fd = pfd;
  96.         ev.events = EPOLLIN;
  97.         std::cout << "adding to epoll list" << std::endl;
  98.         if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, pfd, &ev)) {
  99.             printf("epoll_ctl error %m\n");
  100.             return -1;
  101.         }

  102.     }

  103. }

client.cpp

点击(此处)折叠或打开

  1. //client.cpp
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdio>
  5. extern "C" {
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. }

  11. int main()
  12. {

  13.     const char *servername = "localhost";
  14.     const char *serverport = "59000";

  15.     struct addrinfo server_address;
  16.     memset(&server_address, 0, sizeof(struct addrinfo));
  17.     server_address.ai_family = AF_INET;
  18.     server_address.ai_socktype = SOCK_STREAM;
  19.     server_address.ai_protocol = 0;    // any protocol
  20.     server_address.ai_flags = 0;

  21.     struct addrinfo *result, *rp;

  22.     int res = getaddrinfo(servername, serverport, &server_address, &result);
  23.     if (-1 == res) {
  24.         std::cout << "I cannot getaddress " << servername << std::endl;
  25.         return -1;
  26.     }

  27.     int fd = socket(server_address.ai_family, server_address.ai_socktype, server_address.ai_protocol);
  28.     if (-1 == fd) {
  29.         printf("I cannot open a socket %m\n");
  30.         return -1;
  31.     }

  32.     for (rp = result; rp != NULL; rp = rp->ai_next) {
  33.         std::cout << "************" << std::endl;
  34.         if (-1 == connect(fd, rp->ai_addr, rp->ai_addrlen)) {
  35.             close(fd);
  36.         } else {
  37.             std::cout << "connected" << std::endl;
  38.             break;
  39.         }
  40.     }
  41.     if (rp == NULL) {
  42.         std::cerr << "I couldn't connect server " << servername << std::endl;
  43.     }
  44.     while (true) {
  45.         sleep(2);
  46.         pid_t me = getpid();
  47.         char buf[64];
  48.         bzero(buf, sizeof(buf));
  49.         sprintf(buf, "%ld", me);
  50.         write(fd, buf, sizeof(buf));
  51.         printf("%s\n", buf);
  52.     }
  53. }

g++ -lpthread server.cpp -o server
g++ -lpthread client.cpp -o client

++++++++++++++++++++++++++++++++++++

Answers

A client disconnection is signalled by a EOF condition on the file descriptor. The system considers EOF to be a state in which the file descriptor is 'readable'. But, of course, the EOF condition cannot be read. This is the source of your looping. epoll is acting like the file descriptor for the disconnected client is always readable. You can detect that you have an EOF condition by checking when read returns 0 bytes read.

The only way to deal with an EOF condition is to close the file descriptor in some way. Depending on exactly how the flow of things go, this could be with shutdown(sockfd, SHUT_RD), shutdown(sockfd, SHUT_RDWR) or close(sockfd);.

Unless you know that you need the call for whatever reason, I would recommend you use close. Of course, you should remember to tell epoll that the file descriptor is no longer of interest before you close. I'm not sure what will happen if you don't, but one possibility is that epoll will error. Another is that epoll will mysteriously begin reporting events for a new file descriptor that has the same numeric value before you add it to the list epoll should care about.





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