I O多路轉接(select

2021-09-22 12:36:14 字數 2326 閱讀 2205

一.基本概念

二.函式原型

#includeint select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptions,struct timeval *timeout);
fd_set 介面:

void fd_clr(int fd,fd_set *set);    //用來清除描述片語set中相關fd的位

int fd_isset(int fd,fd_set *set); //用來測試描述片語中相關fd的位是否為真

void fd_set(int fd,fd_set *set); //用來設定描述片語set中相關fd的位

void fd_zero(fd_set* set); //用來清除描述片語中set的全部位

三.select的執行過程

fd_set rfds;     //定義乙個可讀檔案描述符集合

fd_zero(&rfds); //清空可讀檔案描述符的位

fd_set(fd,&rfds); //設定可讀檔案描述符相應位

select(fd+1,&rfds,null,null,null); //只檢測可讀檔案描述符,select阻塞等待

if(fd_isset(fd,&rfds))

socket就緒條件

讀就緒:

寫就緒:

四.select的缺點

五.**

1 #include2 #include3 #include4 #include5 #include6 #include7 #include8 #include9 #include10 

11 12 using namespace std;

13 class sock

14 22 int socket()

23

66 return -1;

67 }

68 69 ~sock()

70

73 };

74 75 #define fd_default -1

76 class fdarray

77 160 else if(s==0)

161

166 else

168

169 }

170 }

171 }

172 }

173

174 ~fdarray()

175

178 };

179

180 void usage(string proc_)

181

184 int main(int argc,char *ar**)

185

190

191 string ip=ar**[1];

192 int port=atoi(ar**[2]);

193 sock listen_sock(ip,port);

194 listen_sock.socket();

195 listen_sock.bind();

196 listen_sock.listen();

197

198 fdarray fd_array(sizeof(fd_set)*8);

199 fd_array.addfd(listen_sock.getsocket());

200

201 for(;;)

202 ;

208 int max_fd=fd_array.maxfd();

209

210 switch(select(max_fd+1,&rfds,null,null,/*&timeout*/null))

221 }

222 }

select多路IO轉接

void fd zero fd set set 清空乙個檔案描述符集合。fd set rset fd zero rset void fd set int fd,fd set set 將待監聽的檔案描述符,新增到監聽集合中 fd set 3,rset fd set 5,rset fd set 6,rs...

IO 多路轉接 select

select多路io轉接 原理 借助核心,select來監聽,客戶端連線 資料通訊事件 void fd clr int fd,fd set set 將乙個檔案描述符從監聽集合中 移除 fd clr 4,rset int fd isset int fd,fd set set 判斷乙個檔案描述符是否在監...

I O多路轉接之select

什麼是i o多路轉接技術 先構建一張有關描述符的列表,然後呼叫乙個函式,直到這些描述符中的乙個已準備好進行i o時,該函式才返回,在返回時,他告訴程序哪些描述符已準備好可以進行i o。上述呼叫的函式,有select,poll,pselect及poll的增強版epoll等,本文主要介紹select。函...