接受/ epoll问题

时间:2009-12-05 16:18:51

标签: linux sockets epoll

我有这个使用epoll的代码,它有问题。当我运行它时,它给出输出: Server-socket()没问题...... Server-bind()没问题...... 3 4 接受:参数无效

我在ubuntu linux上运行它,系统更新了,无论是有限的用户还是root用户 输入接受有什么问题?我应该改变什么?

   struct epoll_event ev, events[MAX_EVENTS];
   struct sockaddr_in serveraddr;
   int listen_sock, conn_sock, nfds, epollfd;
   int yes = 1;

   /* Set up listening socket, 'listen_sock' (socket(),
   bind(), listen()) */

 if((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
 {
  perror("Server-socket() error lol!");
  //just exit lol!
  exit(1);
 }
 printf("Server-socket() is OK...\n");
 //"address already in use" error message 
 /*if(setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) 
 {
  perror("Server-setsockopt() error lol!");
  exit(1);
 }
 printf("Server-setsockopt() is OK...\n");*/
 // bind 
 serveraddr.sin_family = AF_INET;
 serveraddr.sin_addr.s_addr = INADDR_ANY;
 serveraddr.sin_port = htons(PORT);
 memset(&(serveraddr.sin_zero), '\0', 8);
 if(bind(listen_sock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1) 
 {
  perror("Server-bind() error lol!");
  exit(1);
 }
 printf("Server-bind() is OK...\n");

 epollfd = epoll_create(MAX_EVENTS);
 if (epollfd == -1) {
    perror("epoll_create");
    exit(EXIT_FAILURE);
 }

 ev.events = EPOLLIN;
 ev.data.fd = listen_sock;
 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) {
    perror("epoll_ctl: listen_sock");
    exit(EXIT_FAILURE);
 }

 for (;;) {
    nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
    if (nfds == -1) {
     perror("epoll_pwait");
     exit(EXIT_FAILURE);
    }

    for (int n = 0; n < nfds; ++n) {
     if (events[n].data.fd == listen_sock) {
      struct sockaddr_in clientaddr;
      socklen_t addrlen = sizeof(clientaddr);
      cout <<listen_sock <<'\n' <<epollfd <<'\n';
      conn_sock = accept(listen_sock, (struct sockaddr *) &clientaddr, &addrlen);
      if (conn_sock == -1) {
       perror("accept");
       exit(EXIT_FAILURE);
      }
      fcntl(conn_sock, F_SETFL, fcntl(conn_sock, F_GETFD, 0)|O_NONBLOCK);
      ev.events = EPOLLIN | EPOLLET;
      ev.data.fd = conn_sock;
      if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,
         &ev) == -1) {
       perror("epoll_ctl: conn_sock");
       exit(EXIT_FAILURE);
      }
     } else {
      printf("%d\n", events[n].data.fd);
     }
    }
 }

2 个答案:

答案 0 :(得分:7)

你忘了在bind()之后调用listen():

listen(listen_sock, 5);

答案 1 :(得分:-1)

我构建了您的源代码并获得了相同的结果。即使没有任何内容可以接受 epoll_wait 也会立即返回。

似乎不支持使用 epoll_wait 而不是 listen 行为。