zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    数据库信息 (表名 行数 堆 集群 非聚集)的查询
    jquerygalleryview2.0 漂亮多样化的图片特效(多项自定义)
    枚举 EnumDescription 位运算 权限 sql c#
    vs2010缓存类
    透明遮罩层
    app_offline.htm的作用
    XmlToJSON(c#)
    jquery性能最佳实践
    .net面试问答(大汇总)
    apk反编译
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384275.html
Copyright © 2011-2022 走看看