select超时机制失效,这是为什么啊?
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <strings.h>
int main(void)
{
sockaddr_in serverAddr;
int opt = 1;
int len = sizeof(int);
int listenSockfd = -1;
if((listenSockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
{
if(listenSockfd)
{
close(listenSockfd);
listenSockfd = -1;
}
return 0;
}
printf("listenSockfd = %d \n",listenSockfd);
if(setsockopt(listenSockfd, SOL_SOCKET, SO_REUSEADDR, &opt, len) < 0)
{
if(listenSockfd)
{
close(listenSockfd);
listenSockfd = 0;
}
return -1;
}
if (setsockopt(listenSockfd, IPPROTO_TCP, TCP_NODELAY, &opt, len) < 0)
{
return -1;
}
struct timeval recv_timeout;
recv_timeout.tv_sec = 2;
recv_timeout.tv_usec = 0;
setsockopt(listenSockfd, SOL_SOCKET, SO_RCVTIMEO,(char *) &recv_timeout, sizeof(recv_timeout));
int flags;
flags = fcntl(listenSockfd, F_GETFL, 0);
fcntl(listenSockfd, F_SETFL, flags & ~O_NONBLOCK);
bzero(&serverAddr, sizeof(sockaddr_in));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8050);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenSockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr))<0)
{
if(listenSockfd)
{
close(listenSockfd);
listenSockfd = 0;
}
return 0;
}
if(listen(listenSockfd, 5) == -1)
{
if(listenSockfd)
{
close(listenSockfd);
listenSockfd = 0;
}
return 0;
}
/*accept*/
int ClientSocket = 0;
sockaddr_in peerIpAddr;
int socketfd;
socklen_t size = sizeof(sockaddr_in);
bool isAccept = true;
fd_set listenSet;
struct timeval tmVal;
tmVal.tv_sec = 2;
tmVal.tv_usec = 0;
while(isAccept)
{
socketfd = 0;
printf("is accept ...\n");
FD_ZERO(&listenSet);
FD_SET(listenSockfd, &listenSet);
switch (select(listenSockfd + 1, &listenSet, NULL, NULL, &tmVal))
{
case -1:
printf("fail to select\n");
isAccept = false;
return -1;
case 0:
printf("timeout\n");
break;
default:
printf("success\n");
break;
}
}
return 0;
}
本来的意图:设置监听端口为阻塞方式,select设置1s钟超时,每秒钟printf("is accept ...\n");
实际运行结果:不停的打印printf("is accept ...\n");
貌似此时的select的超时机制失效,这是为什么啊?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其实不是的,isaccept 是在做轮询!
struct timeval tmVal;
tmVal.tv_sec = 2;
tmVal.tv_usec = 0;
你把这个时间设定 放在 switch 前面, select 每次结束,会把tmVal 清零,
所以后来每次都是不等待,直接返回。
www.crazyhadoop.com