udp利用select实现超时重传

来源:互联网 发布:网络防御模型ppdr 编辑:程序博客网 时间:2024/06/01 07:31
int maxfdp;
    fd_set fds;
    struct timeval timeout = {1, 0};//设置select等待3秒,3秒轮询,非阻塞就置0
    while(1)
    {
        
        Sendto(skt, sendline, sizeof(vip_hdr) + 100, 0, (SA *)saddr, sizeof(SA));
        FD_ZERO(&fds);//每次循环都要清空集合,否则不能检测描述符变化
        FD_SET(skt, &fds);//添加描述符
        maxfdp = skt + 1;//描述符最大值加1
        switch(select(maxfdp, &fds, NULL, NULL, &timeout))//select的使用
        {
            case -1: 
                exit(-1);
                break;
            case 0:
                timeout.tv_sec = 1;
                 timeout.tv_usec = 0;
                printf("\nhave not recieve net_info packet,send request again!");
                continue;
                break;
            default:
                if(FD_ISSET(skt,&fds))//测试skt是否可读,即网络上是否有数据
                {
            //        printf("\n###########this is ICT 1.1##########################");
                    n = Recvfrom(skt, recvline, MAXLINE, 0,NULL,&len);
                    printf("\n###########this is after recv####%u######################",n);    
                    break;//如果读到数据,退出while循环
                }

        }
    }
0 0
原创粉丝点击