///超时接收处理
int nfound,maxfd=0;
fd_set rset,set;
maxfd = fileno(stdin);
FD_ZERO(&set);//清空套接字集合
FD_SET(sock1,&set);//将sock加入set集合
FD_SET(maxfd,&set);//将maxfd加入set集合
maxfd=(maxfd >sock1?maxfd:sock1)+1;
struct timeval timeout;//超时结构体
timeout.tv_sec=10; //超时为2秒
timeout.tv_usec=0;
rset = set;
if((nfound = select(maxfd,&rset,(fd_set*)0,(fd_set*)0,&timeout))<0)
{
perror("select");
exit(1);
}
if(FD_ISSET(sock1,&rset))用FD_ISSET来检测fileno(stdin)在rset集合中的状态是否变化
{
if((ret = read(sock1,readbuf,sizeof(readbuf)))<0)
{
perror("read");
exit(1);
}
else if(ret ==0)
{
fprintf(stderr,"server disconnect\n");
return -1;
}
readbuf[ret] = '\0';
printf("readbuf:%s\n",readbuf);
return 1;
}
else
{
printf("recv from Server timeout!\n");
return -1;
}
int nfound,maxfd=0;
fd_set rset,set;
maxfd = fileno(stdin);
FD_ZERO(&set);//清空套接字集合
FD_SET(sock1,&set);//将sock加入set集合
FD_SET(maxfd,&set);//将maxfd加入set集合
maxfd=(maxfd >sock1?maxfd:sock1)+1;
struct timeval timeout;//超时结构体
timeout.tv_sec=10; //超时为2秒
timeout.tv_usec=0;
rset = set;
if((nfound = select(maxfd,&rset,(fd_set*)0,(fd_set*)0,&timeout))<0)
{
perror("select");
exit(1);
}
if(FD_ISSET(sock1,&rset))用FD_ISSET来检测fileno(stdin)在rset集合中的状态是否变化
{
if((ret = read(sock1,readbuf,sizeof(readbuf)))<0)
{
perror("read");
exit(1);
}
else if(ret ==0)
{
fprintf(stderr,"server disconnect\n");
return -1;
}
readbuf[ret] = '\0';
printf("readbuf:%s\n",readbuf);
return 1;
}
else
{
printf("recv from Server timeout!\n");
return -1;
}