我正在尝试使用epoll创建非阻塞连接。我仍在学习epoll,因此我尝试使用此示例中提到的代码-How to code an epoll based sockets client in C。
我刚刚修改了超时并将事件设置为EPOLLOUT。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 20000
#define SERVER "127.0.0.1"
#define MAXBUF 1024
#define MAX_EPOLL_EVENTS 64
int main() {
int sockfd;
struct sockaddr_in dest;
char buffer[MAXBUF];
struct epoll_event events[MAX_EPOLL_EVENTS];
int i, num_ready;
/*---Open socket for streaming---*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0)) < 0 ) {
perror("Socket");
exit(errno);
}
/*---Add socket to epoll---*/
int epfd = epoll_create(1);
struct epoll_event event;
event.events = EPOLLOUT; // Cann append "|EPOLLOUT" for write events as well
event.data.fd = sockfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT);
if ( inet_pton(AF_INET, SERVER, &dest.sin_addr.s_addr) == 0 ) {
perror(SERVER);
exit(errno);
}
/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) {
if(errno != EINPROGRESS) {
perror("Connect ");
exit(errno);
}
}
/*---Wait for socket connect to complete---*/
num_ready = epoll_wait(epfd, events, MAX_EPOLL_EVENTS, 100000/*timeout*/);
for(i = 0; i < num_ready; i++) {
if(events[i].events & EPOLLOUT) {
printf("Socket %d connected\n", events[i].data.fd);
}
}
/*---Wait for data---*/
num_ready = epoll_wait(epfd, events, MAX_EPOLL_EVENTS, 100000);
for(i = 0; i < num_ready; i++) {
if(events[i].events & EPOLLOUT) {
printf("Socket %d got some data\n", events[i].data.fd);
//bzero(buffer, MAXBUF);
//recv(sockfd, buffer, sizeof(buffer), 0);
//printf("Received: %s", buffer);
}
}
close(sockfd);
return 0;
}
结果:
[root@test-vm socket_prgm]# ./test_epoll_so.o
Socket 3 connected
Socket 3 got some data
我在上述程序中观察到的两件事:
我在这里错过明显的东西吗?谢谢。
我认为确实没有找到明显的地方:
2.The timeout period does not work at all. The script executes immediately.
它会立即执行,因为您已连接到本地主机。当EINPROGRESS
极不可能返回的情况下,这是一种特殊情况,因为可以立即建立连接。建立连接后,文件描述符将变为可写状态,并且epoll_wait
返回该事件,因此大家都可以继续进行。如果与远程主机的三向握手时间超过100秒,它将等待您的超时。
1. There is no server process running on the port 20000. The socket was still able to connect for the event EPOLLOUT.
这是您想念的:您的代码中没有任何东西使其成为一个过程。 fork
返回后,您必须main()
或使用线程离开进程。