计算机函数select,请问关于select函数的操作程序?
使用select函數(shù)的部分代碼如下:
//設(shè)置超時(shí)時(shí)間
timeval *ptimeval = new timeval;
ptimeval.tv_sec = 60;
ptimeval.tv_usec = 10;
m_Exit = FALSE;
while( m_Exit != TRUE)
{
select( maxfds, &readfds, &writefds, &exceptfds,ptimeval);
cout << “ time is out…”<< endl;
);
現(xiàn)象:第一次可以等待60秒后,退出Select函數(shù),但是第二次進(jìn)入Select函數(shù)后,瞬間就會(huì)退出,根本不會(huì)等待60秒,屏幕上“time is out"不間斷的出現(xiàn)
原因:調(diào)用select之后,readfds的fd_count值由1變?yōu)?,所以瞬間返回,每次將readfds的fd_count值設(shè)為1,既每次用FD_SET來重置讀集合,則功能正常實(shí)現(xiàn)
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() */
}
}
}
總結(jié)
以上是生活随笔為你收集整理的计算机函数select,请问关于select函数的操作程序?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++之运算符重载(上)
- 下一篇: [Nikon D80]樱花盛开的校园