Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在TCP服务器上实现poll();s读/写_C_Sockets_Client Server_Polling - Fatal编程技术网

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

在TCP服务器上实现poll();s读/写,c,sockets,client-server,polling,C,Sockets,Client Server,Polling,我需要这个服务器能够监听和建立新的客户端连接,同时写入现有的连接。。即异步非阻塞i/o。我被告知要使用poll(),但在花了大量时间试图掌握socket编程之后,我仍然不确定如何实现poll()函数 int sockfd; int main(int argc, char *argv[]) { int newsockfd, portno; socklen_t clilen; char buffer[256]; struct sockaddr_in serv_addr

我需要这个服务器能够监听和建立新的客户端连接,同时写入现有的连接。。即异步非阻塞i/o。我被告知要使用poll(),但在花了大量时间试图掌握socket编程之后,我仍然不确定如何实现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; 

和使用投票(ufds,22000);在循环内部检查sockfd或newsockfd是否有任何活动,在这种情况下,我使用适当的读或写。。如果有人能给我一些指导,我将非常感激

为什么不使用libevent?它完全异步且无阻塞。

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

从手册页面:

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

如果要为已接受的连接发送事件通知,则需要提前为每个连接预留空间或调整
struct pollfd
数组的大小

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

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 */
}
inti,n;
n=轮询(ufds,数组中的num\u fds\u,超时值);
/*错误还是超时*/
if(n<1)
;
对于(i=0;i
POLLOUT
标志用于在套接字上发送数据不会阻止调用方时发出信号

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


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

您是否阅读过“选择”相关内容?如果您能展示如何将此代码应用于OPs代码,我将非常高兴。我有完全相同的问题,我读()命令,它阻止正确,但我想有一个连接超时。我试了很多东西,但都没用。我不明白你的密码。。。什么是UFD?这是新闻吗??
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 */
}