select()函数
1、利用select()函数进行超时检测相比较于其他两种方式的特点是:
使用select函数实现超时检测,超时时间设置一次,只会有效一次,所以需要将其放在循环里面
当select函数到达设定的时间时,函数会返回0
2、在进行超时检测时,应注意的是select的返回值。如果利用其进行超时检测,第5个参数必然不能为 NULL ,在其不为空时,超时后返回 0 。
使用select()实现超时检测
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
功能:允许一个进程去操作多个文件描述符,阻塞等待一个或者是多个文件描述符准备就绪,
当有一个或者多个文件描述符准备就绪,则函数立即返回
void FD_CLR(int fd, fd_set *set);
将fd移除set集合
int FD_ISSET(int fd, fd_set *set);
判断fd是否存在在set里面
void FD_SET(int fd, fd_set *set);
将fd添加到set集合里面
void FD_ZERO(fd_set *set);
清空set集合
nfds: 最大的文件描述符加一
readfds: 读文件描述符集合
writefds:写文件描述符集合
exceptfds:其他或者异常文件描述符集合
timeout: 设置超时时间
如果timeout = NULL 则返回准备就绪的文件描述符的个数
如果timeout != NULL 超时后返回0
+++++++++++++++++++++++++++++++++++++
struct timeval {
int tv_sec; 秒
int tv_usec; 微秒
+++++++++++++++++++++++++++++++++++++
struct timeval out_time;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
out_time.tv_sec = 5;
out_time.tv_usec = 0;
if((num = select(maxfd + 1, &readfds, NULL, NULL, &out_time)) < 0)
errlog("fail to select");
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
下面上实例:
前面我们用select()函数实现并发服务器,在原来代码的基础上我们稍加更改,便可实现超时检测的功能。
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#define N 128
#define errlog(errmsg) do{perror(errmsg); exit(1);}while(0)
int main(int argc, const char *argv[])
int sockfd;
struct sockaddr_in serveraddr, clientaddr;
int acceptfd;
socklen_t addrlen = sizeof(struct sockaddr_in);
fd_set readfds;
int maxfd;
int num = 0;
fd_set tempfds;
int i = 0;
char buf[N] = {};
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
errlog("fail to socket");
printf("sockfd = %d\n", sockfd);
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
serveraddr.sin_port = htons(atoi(argv[2]));
if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
errlog("fail to bind");
if(listen(sockfd, 10) < 0)
errlog("fail to listen");
struct timeval out_time;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
maxfd = sockfd;
while(1)
out_time.tv_sec = 5;
out_time.tv_usec = 0;
tempfds = readfds;
if((num = select(maxfd + 1, &tempfds, NULL, NULL, &out_time)) < 0)
errlog("fail to select");
else if(num == 0)
printf("timeout...\n");
for(i = 0; i < maxfd + 1; i++)
if(FD_ISSET(i, &tempfds) == 1)
if(i == sockfd)
if((acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen)) < 0)
errlog("fail to accept");
printf("acceptfd = %d\n", acceptfd);
printf("%s ---> %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
FD_SET(acceptfd, &readfds);
maxfd = maxfd > acceptfd ? maxfd : acceptfd;
if(recv(i, buf, N, 0) < 0)
errlog("fail to recv");
if(strncmp(buf, "quit", 4) == 0)
printf("%s is quited...\n", inet_ntoa(clientaddr.sin_addr));
break;
printf("from client >>> %s\n", buf);
strcat(buf, " from server...");
send(i, buf, N, 0);
close(sockfd);
close(acceptfd);
return 0;
select()函数 1、利用select()函数进行超时检测相比较于其他方式的特点是: 使用select函数实现超时检测,超时时间设置一次,只会有效一次,所以需要将其放在循环里面 2、在进行超时检测时,应注意的是select的返回值。如果利用其进行超时检测,第5个参数必然不能为 NULL ,在其不为空时,超时后返回 0 。使用select()实现超时检测 #inclu
int ReadTimeOut(int nSocket, unsigned int nWaitSeconds) {
//默认为0,当wait_seconds==0时,不检测直接返回0
int nRet = 0;
//需要检测超时
if (nWaitSeconds > 0) {
fd_set stReadSocketSet; //读套接字结构体
struct timeval stTim
传统的recvfrom是阻塞进行的,即调用recvfrom之后程序就会阻塞,等待数据包的到来,如果没有数据包,程序就永远等待。
在很多场景中,我们需要设置超时参数,如果该套接口超时之后仍然没有数据包到来,那么就直接返回。
socket编程中这样的超时机制可以使用select和recvfrom这两个函数实现
实现代码如下
关于select函数超时时间问题
select函数使用
int select(int n,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout);
具体参数说明:
1、 int n是一个整数值,是指集合中所有文件描述符的范围,即所有文件描述符的最大值加1,不能错!在Windows中这个参数的值无所谓,可以设置不正确。
2、fd_set *readfds是指向fd_set结构的指针,这个集合中应该包括文件
开发过程中用到了select函数监听多个描述符状态的变化,转载过一篇《select函数详解及应用》的文章,在使用过程中遇到过一此这篇文章中没有提到的问题,现做一下总结。 函数原型为:
int select(intmaxfdpl, fd_set * readfds, fd_set *writefds,
fd_set * read exceptfds, stru
<br />如果网络出现异常,TCP连接中断,那么计算机要多久才能够检测出来呢?
最佳答案 这个问题得分情况来说:
1、双方只建立了一个连接,然后就再也没有任何数据通信,这完全是可能的。这时,tcp有一个保活定时器,它的作用就是确认对端是否存活,通常它没2个小时发送一个数据包给对方,以查看是否有响应。
2、tcp中断,此时发送端发送数据,但是由于收不到ACK,它会一直尝试,通常的时间间隔为1s, 3s, 6s, 12s ..... 64s,这个间隔被称为"指数退避",到最后,通常在9分钟