在服务器侧,使用select检查新建的socket是否可读写,超时时间设置为5秒, 但由于采用了多线程,如果在接收线程进入到select后,其他线程中shutdown和close掉这个建立的socket后, select没有立即返回,请问是怎么回事? 谢谢了
看到linux2.4源码里是计算三个集的事件总数返回,我自己写的一个小例子也发现是事件数。 可是,在我一个项目里,当某个套接字同时发生可读和异常时,发现并不计入返回值里。就是我有800多个套接字,分别关注了其读(或写)和异常,某次select发现返回1,但具体FD_ISSET时却只其中一个是可写,其它300多个是可读和异常同时置值。 另外,看lighttpd的源码fdevent_select,也发现它只是认为是发生的句柄数并用它作循环。(当然,它...
#include
比如下面的例子: int sockfd; fd_set fdR; struct timeval timeout = ..; ... for(;;) { FD_ZERO(&fdR); FD_SET(sockfd, &fdR); switch (select(sockfd + 1, &fdR, NULL, &timeout)) { case -1: error handled by u; case 0: timeout hanled by u; default: if (FD_ISSET(sockfd)) { now u read or recv something; /* if sockfd is father and server socket, u can now accept() */ } } } 我根据select的返回值大于0,已经得到...
/*select每次都返回1,sockfd[0]从来没有通过FD_ISSET,而其余4个每次都通过*/ timeout.tv_sec=0; timeout.tv_usec=usec; FD_ZERO(&udpfdset); FD_SET(sockfd[0],&udpfdset); FD_SET(sockfd[1],&udpfdset); FD_SET(sockfd[2],&udpfdset); FD_SET(sockfd[3],&udpfdset); FD_SET(sockfd[4],&udpfdset); selfd=select(Maxfd,&udpfdset,NULL,NULL,&timeout); printf("s%dn",selfd); ...
Quite often when you’re writing Linux applications, you may need to examine the state of a number of inputs to determine the next action to take. For example, a communication program such as a terminal emulator needs to read the keyboard and the serial port effectively at the same time. In a single-user system, it might be acceptable to run in a “busy wait” loop, repeatedly scanning the input f...