Linux高性能服务器之多路转接(2)---poll模型

来源:互联网 发布:网络教育退学 编辑:程序博客网 时间:2024/06/14 07:13
#include <stdio.h>  #include <stdlib.h>  #include <unistd.h>  #include <netinet/in.h>  #include <arpa/inet.h>  #include <unistd.h>  #include <string.h>   #include <poll.h>  #include <sys/socket.h>  #include <sys/types.h>  #include <sys/stat.h>    #define POLLFD_SIZE 1024 /* struct pollfd 结构体数组最大上限 */    struct pollfd array_pollfd[POLLFD_SIZE];    /* 结构体成员详情 struct pollfd  {     int fd;        // 关心的描述符     short events;  // 关心的事件     short revents; // 发生的事件 }; */   int startup(const char* ip, int port)  {      int sock = socket(AF_INET, SOCK_STREAM, 0);      if( sock < 0){          perror("socket()");          exit(1);      }        int opt = 1;      setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));        struct sockaddr_in local;       local.sin_addr.s_addr = inet_addr(ip);      local.sin_port = htons(port);      local.sin_family = AF_INET;        if(bind(sock, (struct sockaddr *)&local, sizeof(local) ) < 0){          perror("bind()");          exit(2);      }        if(listen(sock, 5) < 0){          perror("listen()");          exit(3);      }        return sock;  }  void run_poll(int listen_sockfd);    void run_accept(int listen_sock);    void run_action( int index);    int main(int argc, char **argv)  {      if(argc != 3)      {          printf("usage: %s[local_ip][local_port]", argv[0]);          return 1;      }        int listen_sockfd = startup(argv[1], atoi(argv[2]));            array_pollfd[0].fd = listen_sockfd;      array_pollfd[0].events = POLLIN;      int idx_init = 1;      for(; idx_init < POLLFD_SIZE; ++idx_init)      {          array_pollfd[idx_init].fd = -1;      }      int timeout = 1000;        while(1)      {          int ret_poll = poll(array_pollfd,  POLLFD_SIZE, timeout);            if(ret_poll == 0)               printf("timeout\n");          else if(ret_poll < 0)              perror("poll()");          else          {            int idx_check = 0;              for(idx_check = 0; idx_check < POLLFD_SIZE; ++idx_check)              {                  if(idx_check == 0 && array_pollfd[0].revents & POLLIN)                  {                     run_accept(listen_sockfd);                   }                  else if(idx_check != 0)                  {                    run_action(idx_check);                  }              }          }      }    return 0;  }     void run_action( int index)  {      if(array_pollfd[index].revents & POLLIN)      {        char buf[1024];            memset(buf, 0, sizeof(buf));          ssize_t s = read(array_pollfd[index].fd, buf, sizeof(buf)-1);          if(s > 0)          {              buf[s-1] = '\0';              printf("client say$ %s \n", buf);              array_pollfd[index].events = POLLOUT;          }          else if( s <= 0)          {              printf("client quit!\n");              close(array_pollfd[index].fd);              array_pollfd[index].fd = -1;          }        }      else if (array_pollfd[index].revents & POLLOUT)      {        const char* msg = "HTTP/1.1 200 OK\r\n\r\n<html><br/><h1>Hello poll!</h1></html>";          write(array_pollfd[index].fd, msg, strlen(msg));              close(array_pollfd[index].fd);          array_pollfd[index].fd = -1;      }  }    void run_accept(int listen_sock)  {      struct sockaddr_in cliaddr;      socklen_t clilen = sizeof(cliaddr);        int new_sock = accept(listen_sock, (struct sockaddr*)&cliaddr, &clilen);      if( new_sock < 0)      {          perror("accept");          return ;      }        printf("与客户端连接成功: ip %s port %d \n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));      int idx_find = 1;      for(; idx_find < POLLFD_SIZE; ++idx_find)      {          if(array_pollfd[idx_find].fd < 0)          {              array_pollfd[idx_find].fd = new_sock;              array_pollfd[idx_find].events = POLLIN ;              break;          }      }         if(idx_find == POLLFD_SIZE)      {          perror("连接超出最大限度,add array_pollfd[]");          return;      }    }    

阅读全文
0 0
原创粉丝点击