在TCP服务器的读/写上实现poll()

ach*_*ung 5 c sockets client-server polling

我需要这个服务器能够监听并与客户建立新的连接,同时写入现有的连接..即.异步非阻塞i/o.我被告知使用poll()但是在花费了大量时间之后只是试图掌握套接字编程,我仍然不确定如何实现poll()函数.

int sockfd; 

int main(int argc, char *argv[])
{
 int newsockfd, portno; 
 socklen_t clilen;  
 char buffer[256];       
 struct sockaddr_in serv_addr, cli_addr;  
 int n;             

 if (argc < 2) {         
     fprintf(stderr,"ERROR, no port provided\n"); 
     exit(1);
 }

 sockfd = socket(AF_INET, SOCK_STREAM, 0);      
 if (sockfd < 0)                    
    error("ERROR opening socket");          

 bzero((char *) &serv_addr, sizeof(serv_addr));     

 portno = atoi(argv[1]);            
 serv_addr.sin_family = AF_INET;        
 serv_addr.sin_addr.s_addr = INADDR_ANY;    
 serv_addr.sin_port = htons(portno);    

 if (bind(sockfd, (struct sockaddr *) &serv_addr, 
          sizeof(serv_addr)) < 0) 
          error("ERROR on binding");        
 listen(sockfd,5);                  

 clilen = sizeof(cli_addr);     

 while(1){          
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);              
     if (newsockfd < 0)                 
          error("ERROR on accept");

     // READ READ READ READ READ READ READ READ READ READ READ READ READ READ READ READ 
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);    


     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);

     // WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE WRITE 
     n = write(newsockfd,"I got your message",18); 
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);  
 }

 return 0; 
Run Code Online (Sandbox Code Playgroud)

}

我的理解是我需要构建这样的东西:

//  Set up array of file descriptors for polling
struct pollfd ufds[2];
ufds[0].fd = sockfd;    
ufds[0].events = POLLIN; 

ufds[1].fd = newsockfd;
ufds[1].events = POLLOUT;
Run Code Online (Sandbox Code Playgroud)

并使用民意调查(ufds,2,2000); 在循环内部检查sockfd或newsockfd是否有任何活动,在这种情况下我使用适当的读或写..如果有人可以给我一些指导我会非常感激.

has*_*ste 4

revents内核将填充数组字段中发生的事件struct pollfd

从手册页:

revents 字段是一个输出参数,由内核填充实际发生的事件。revents 中返回的位可以包括事件中指定的任何位,或者值 POLLERR、POLLHUP 或 POLLNVAL 之一。(这三个位在 events 字段中没有意义,只要相应的条件为真,就会在 revents 字段中设置。)

如果您想要接受连接的事件通知,那么您需要提前预留空间或struct pollfd为每个连接调整数组大小。

您需要某种方法来区分侦听套接字。您可以将其存储在数组的索引零中。

int i, n;

n = poll(ufds, num_fds_in_array, timeout_value);

/* errors or timeout? */
if (n < 1)
    ;

for (i = 0; i < num_fds_in_array; i++) {
    /* were there any events for this socket? */
    if (!ufds[i].revents)
        continue;

    /* is it our listening socket? */
    if (!i) {
         if (ufds[0].revents & POLLIN)
             /* call accept() and add the new socket to ufds */
         else
             /* error */

         continue;
    }

    /* is there incoming data on the socket? */
    if (ufds[i].revents & POLLIN)
        /* call recv() on the socket and decide what to do from there */
}
Run Code Online (Sandbox Code Playgroud)

POLLOUT标志用于在套接字上发送数据不会阻塞调用者时发出信号。

对于非阻塞 I/O,我会使用更强大的 API,因为它需要更多的簿记才能可靠地完成。请参阅下一段。

不幸的是,在使用时没有空间用于辅助每个连接数据来存储状态poll。根据您的平台,有一些可用的替代方案,例如适用于 Linux 的 epoll、适用于 *BSD 的 kqueue 以及适用于 Windows 的一些选项。如果要使用poll上下文数据,则必须使用可以使用文件描述符或数组索引进行搜索的数据结构。