接受/epoll问题

发布于 2024-08-13 08:34:43 字数 2404 浏览 9 评论 0原文

我有这段使用 epoll 的代码,但有问题。当我运行它时,它给出输出: 服务器套接字()没问题...... 服务器绑定()是好的... 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);
     }
    }
 }

I have this code that uses epoll and it has a problem. When I run it, it gives output:
Server-socket() is OK...
Server-bind() is OK...
3
4
accept: Invalid argument

I'm running it on ubuntu linux, system updated, both as limited user and root
what is wrong with the input to accept? What should I change?

   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);
     }
    }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-08-20 08:34:43

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

listen(listen_sock, 5);

You forgot to call listen() after bind():

listen(listen_sock, 5);
念﹏祤嫣 2024-08-20 08:34:43

我建立了你的来源并得到了相同的结果。即使没有任何东西可以接受epoll_wait也会立即返回。

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

I built your source and had the same results. The epoll_wait returns immediately even though there is nothing to accept.

It appears to be the case that using epoll_wait instead of listen is not supported behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文