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

时间:2023-03-09 22:15:00
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

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

本人微信:xiaoshitou5854