利用select函數(shù)可以完成非阻塞通信,它可以在用戶指定的時(shí)間內(nèi)監(jiān)聽一些句柄(文件描述符?)(一個(gè)句柄就是你給一個(gè)文件,設(shè)備,套接字(socket)或管道的一個(gè)名字, 以便幫助你記住你正處理的名字, 并隱藏某些緩存等的復(fù)雜性。),當(dāng)readfds或writefds中映象的文件可讀或可寫或超時(shí),本次select()就結(jié)束返回。程序員利用一組系統(tǒng)提供的宏在select()結(jié)束時(shí)便可判斷哪一文件可讀或可寫。
。
函數(shù)原型:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
ndfs:select監(jiān)視的文件句柄數(shù),視進(jìn)程中打開的文件數(shù)而定,一般設(shè)為你要監(jiān)視各文件
中的最大文件號(hào)加一。
readfds:select監(jiān)視的可讀文件句柄集合。
writefds: select監(jiān)視的可寫文件句柄集合。
exceptfds:select監(jiān)視的異常文件句柄集合。
timeout:本次select()的超時(shí)結(jié)束時(shí)間。
函數(shù)返回值:
0表示超時(shí)
-1表示出錯(cuò)
正數(shù)表示文件可讀或可寫
相關(guān)的宏:
FD_ZERO(fd_set *fdset):清空fdset與所有文件句柄的聯(lián)系。
FD_SET(int fd, fd_set *fdset):建立文件句柄fd與fdset的聯(lián)系。
FD_CLR(int fd, fd_set *fdset):清除文件句柄fd與fdset的聯(lián)系。
FD_ISSET(int fd, fd_set *fdset):檢查fdset聯(lián)系的文件句柄fd是否可讀寫,當(dāng)>0表示可讀寫。
select只能監(jiān)聽文件描述符(file descriptors),文件指針是不行的。通過int fileno(FILE* fp)函數(shù)可以完成文件指針到文件描述符的轉(zhuǎn)換。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>
int main(){
int fno = fileno(stdin);
// 得到sdtin的文件描述符
fd_set fdR;
struct timeval timeout = {.tv_sec = 10, .tv_usec = 0};
FD_ZERO(&fdR);
FD_SET(fno, &fdR);
char str[64];
int goOn = 1;
while(goOn){
printf("goOning
\n");
switch(select(fno + 1, &fdR, NULL, NULL, &timeout)){
case -1:
printf("select fail\n");
goOn = 0;
break;
case 0:
printf("select end\n");
goOn = 0;
break;
default:
scanf("%s", str);
printf("str=%s\n", str);
}
}
}
代碼片段1:在指定時(shí)間內(nèi)讀取stdin的數(shù)據(jù)。
代碼片段2:服務(wù)器在一定時(shí)間內(nèi)接收子節(jié)點(diǎn)發(fā)來的數(shù)據(jù)報(bào)。
int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
…

…

fd_set fdR;
struct timeval timeout = {tv_sec:10, tv_usec:0};// 等待接收一定時(shí)間之后結(jié)束
int goOn = 1;
FD_ZERO(&fdR);
FD_SET(sockfd, &fdR);
while(goOn){
switch(select(sockfd + 1, &fdR, NULL, NULL, &timeout)){
case -1:
printf("select fail\n");
goOn = 0;
break;
case 0:
printf("select timeout\n");
goOn = 0;
break;
default:
// 正常情況,讀取信息
if((numbytes = recvfrom(sockfd, &cdt, sizeof(cdt), 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1){
printf("recvfrom fail\n");
}
}
posted on 2014-03-26 16:25
小鼠標(biāo) 閱讀(3979)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
本科畢設(shè)