select 实例
时间:2009-07-22 来源:sure_priterand
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[100]="";
struct timeval timeout;
fd_set rset;
FD_ZERO(&rset);
FD_SET(0, &rset);
FD_SET(2,&rset);
FD_SET(4,&rset);
while(1)
{
timeout.tv_sec = 2;//设置超时时间为2秒
timeout.tv_usec = 0;//设置超时时间0微秒
if(select(1, &rset, NULL, NULL, &timeout)>0)
{//如果在超时时间内(即 没超时)
read(0, buf, 100);
printf("buf=%s\n",buf);
}
else
return 0;
}
return 0;
}
#include <stdlib.h>
#include <string.h>
int main()
{
char buf[100]="";
struct timeval timeout;
fd_set rset;
FD_ZERO(&rset);
FD_SET(0, &rset);
FD_SET(2,&rset);
FD_SET(4,&rset);
while(1)
{
timeout.tv_sec = 2;//设置超时时间为2秒
timeout.tv_usec = 0;//设置超时时间0微秒
if(select(1, &rset, NULL, NULL, &timeout)>0)
{//如果在超时时间内(即 没超时)
read(0, buf, 100);
printf("buf=%s\n",buf);
}
else
return 0;
}
return 0;
}
相关阅读 更多 +