zoukankan      html  css  js  c++  java
  • (OK) pthread—epoll-loops-on-disconnection-of-a-client—server

    http://stackoverflow.com/questions/14563134/epoll-loops-on-disconnection-of-a-client

    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 ");
    22.         } else if (retpw == 0) {
    23.             printf("nothing is ready yet ");
    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 ");
    32.                     }
    33.                     printf("%s ", 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 ");
    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 ");
    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 ");
    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 ");
    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 ", 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 shutdown(2) 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.






  • 相关阅读:
    使用Jenkins自带功能(不用shell)构建Docker镜像并推送到远程仓库
    方法2:使用Jenkins构建Docker镜像 --SpringCloud
    在jenkins中使用shell命令推送当前主机上的docker镜像到远程的Harbor私有仓库
    解决跟Docker私有仓库登陆,推送,拉取镜像出现的报错
    Linux 内存占用大排查
    方法1:使用Jenkins构建Docker镜像 --SpringCloud
    使用Jenkins编译打包SpringCloud微服务中的个别目录
    使用Jenkins的Git Parameter插件来从远程仓库拉取指定目录的内容
    稀疏检出-使用git检索出仓库里的某一个目录文件,而不是整个仓库的所有文件
    通过 Kubeadm 安装 K8S 与高可用,版本1.13.4
  • 原文地址:https://www.cnblogs.com/ztguang/p/12647125.html
Copyright © 2011-2022 走看看