IO五种模式

IO五种模式


 * 阻塞I|O 
  非阻塞模式  fcntl(fd , F_SETFL , flag |O_NONBLOCK)

  int select(int nfds,fd_set *readfds ,fd_set* writefds , 
                fd_set*exceptfds , struct timeval* timeout )

    void FD_CLR(int fd , fd_set* set);  删除指定                    
    void FD_SET(int fd ,fd_set* set); 添加
    void FD_ISSET(int fd , fd_set* set); 查询
    void FD_ZERO(fd_set * set); 置0

close and shutdown:
    close 终止数据传送的两个方向

#include<sys/socket.h>
    int shutdown(int sockfd , int HOW);

    HOW :
        SHUT_RD  关闭RD
        SHUT_WR  关闭WR
        SHUT_RDWR  全关

select 实现超时:  函数的封装
        read_timeout 
        write_timeout 
        accept_timepout 
        connect_timeout

    int read_timeout(int fd , unsigned int wait_seconds){
    int ret = 0;

    if(wait_seconds > 0 ){
        fd _set  readfd;
        struct timeval timeout ;

        FD_ZERO(&readfd);
        FD_SET(fd ,&readfd);

        timeout.tc_sec = wait_sconds
        timeout.tc_usec = 0;

        do{
            ret = select (fd+1 ,&readfd , NULL  ,NULL ,&timeout);

        }while(ret < 0 && errno == EINTR);

        if(ret == 0){
            ret = -1;
            errno = ETIMEDOUT;
        }
        else if(ret > 0){
            ret = 0;
        }   
        return ret ;    
    }
    return -1;  
}

===---------------connect_timeout--------------------
void activate_nonblcok(int fd){
    int  ret ;
    int flags = fcntl(fd , F_GETFL);
    if(flags == -1){
        err_exit("fcntl");
        flags |= O_NONBLOCK ;
        ret = fcntl(fd , F_SETFL .flags);
        if(ret == -1){
            err_exit("fcntl");  
        }
    }

}

int connect_time(int fd , struct sockaddr* addr ,unsigned int wait_seconds){
    int ret = 0;
    socklen_t addrlen;

}
原文地址:https://www.cnblogs.com/Sico2Sico/p/5384275.html