1. 程式人生 > >c/c++ llinux epoll系列4 利用epoll_wait實現非阻塞的connect

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\n");
      
      nfds = epoll_wait(epfd, ev_ret, EVENTS, 1000*10);//timeout is 1 sec
      if(nfds < 0){
    perror("epoll_wait\n");
    return 1;
      }
      printf("after epoll_wait : nfds=%d\n", 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