zoukankan      html  css  js  c++  java
  • c/c++ llinux epoll系列4 利用epoll_wait实现非阻塞的connect

    llinux epoll系列4 利用epoll_wait实现非阻塞的connect

    connect函数是阻塞的,而且不能设置connect函数的timeout时间,所以一旦阻塞太长时间,影响用户的体验,所以就出来一个需求,硬要设置connect的timeout时间。

    实现方法:先把connect函数变成非阻塞的,然后用设置epoll_wait的timeout时间,用epoll_wait等待connect的完成。

    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <sys/epoll.h>
    #include <sys/ioctl.h>
    #include <errno.h>
    #include <arpa/inet.h>
    #include <fcntl.h>
    
    #define EVENTS 10
    
    int main(){
      sockaddr_in server;
      int sock, epfd;
      char buf[32];
      int nfds, n;
      int val;
      epoll_event ev, ev_ret[EVENTS];
    
      sock = socket(AF_INET, SOCK_STREAM, 0);
    
      server.sin_family = AF_INET;
      server.sin_port = htons(12345);
      inet_pton(AF_INET, "127.0.0.1", &server.sin_addr.s_addr);
    
      epfd = epoll_create(1);
      if(epfd < 0){
        perror("epfd");
        return 1;
      }
    
      memset(&ev, 0, sizeof(ev));
      ev.events = EPOLLIN;
      ev.data.fd = sock;
    
      if(epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ev) != 0){
        perror("epoll_ctl");
        return 1;
      }
    
      val = 1;
      //本来下面的connect函数是阻塞的,但是用FIONBIO的ioctl函数后,就变成非阻塞的了,
      //所以不管connect函数成功与否,都立即结束了。
      ioctl(sock, FIONBIO, &val);
      
      n = connect(sock, (sockaddr*)&server, sizeof(server));
    
      if(n != 0){
        if(errno == EINPROGRESS){
          printf("before epoll_wait
    ");
          
          nfds = epoll_wait(epfd, ev_ret, EVENTS, 1000*10);//timeout is 1 sec
          if(nfds < 0){
    	perror("epoll_wait
    ");
    	return 1;
          }
          printf("after epoll_wait : nfds=%d
    ", nfds);
        }
        else{
          perror("connect");
          return 1;
        }
      }
    
      n = read(sock, buf, sizeof(buf));
      write(fileno(stdout), buf, n);
    
      close(sock);
    
      return 0;
      
    }
    
    

    github源代码

    但是,虽然成功的让connect函数变成了非阻塞的了,但是也让epoll_wait函数也变成非阻塞的,不管怎么设置epoll_wait的timeout,都不起作用,程序瞬间的完成。求高人指点!!!!

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    sudo命令 sudoers文件
    sscanf函数
    printf格式化输出
    c文件操作
    string和char*
    c去除空格 小写转大写
    主机序和网络序转换
    ulimit用法
    mysql基础(附具体操作代码)
    ES6 class
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9827277.html
Copyright © 2011-2022 走看看