网络超时检测-select()函数

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
        失败:
            -1
+++++++++++++++++++++++++++++++++++++
     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>

//使用select函数实现TCP并发服务器

#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);

    //填充网络信息结构体
    //inet_addr 将点分十进制转化成网络字节
    //htons表示将主机字节序转化成网络字节序
    //atoi 将字符串转化成整型数据
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    serveraddr.sin_port = htons(atoi(argv[2]));

    //将套接字与IP地址和端口号绑定
    if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    {
        errlog("fail to bind");
    }

    //将套接字设置为被动监听状态
    if(listen(sockfd, 10) < 0)
    {
        errlog("fail to listen");
    }

    //使用select函数实现超时检测
    //超时时间设置一次,只会有效一次,所以需要将其放在循环里面

    struct timeval out_time;
    //第一步:清空集合
    FD_ZERO(&readfds);

    //第二步:将文件描述符添加到集合当中
    //注意:当select函数调用成功后,他会清除没有准备就绪的文件描述符,所以需要每次重复添加
    FD_SET(sockfd, &readfds);

    maxfd = sockfd;

    while(1)
    {
        out_time.tv_sec = 5;
        out_time.tv_usec = 0;

        tempfds = readfds;

        //第三步:调用select函数将添加进去的文件描述符准备就绪
        if((num = select(maxfd + 1, &tempfds, NULL, NULL, &out_time)) < 0)
        {
            errlog("fail to select");
        }
        else if(num == 0)
        {
            printf("timeout...\n");
        }
        else
        {
            //使用FD_ISSET判断文件描述符
            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;

                    }
                    else
                    {

                        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;
                        }
                        else
                        {
                            printf("from client >>> %s\n", buf);

                            strcat(buf, " from server...");

                            send(i, buf, N, 0);

                        }

                    }   
                }
            }
        }
    }

    close(sockfd);
    close(acceptfd);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值