epoll小节
一. 参考博客
1. 讲解流、I/O、阻塞/非阻塞,select/epoll的异同点。
3. 水平触发和边沿触发
二. 水平触发和边沿触发
1. 水平触发:对于监听的套接字可读事件来说,只要套接字(udp和tcp)的缓冲区上有数据epoll就会一直通知套接字可读。
1.1 比如1:tcp发送端发送了100字节,epoll_wait唤醒后仅读取了50字节,因为缓冲区中仍有数据可读,第二次调用epoll_wait时,调用进程仍然会被唤醒。
1.2 比如2:tcp(udp)发送进程的发送频率大于接收频率,发送一段时间后,发送进程退出,接收进程仍能继续读出发送方退出前发送且未读的数据。
2. 边沿触发:对于监听的套接字可读事件来说,epoll_wait仅被唤醒一次。
2.1 比如1:tcp发送端发送了100字节,epoll_wait唤醒后仅读取了50字节,第二次调用epoll_wait时,调用进程不会被唤醒,缓冲区中的数据只有等到epoll_wait下次被唤醒时才能读出来。
2.2 比如2:tcp(udp)发送进程的发送频率大于接收频率,发送一段时间后,发送进程退出,接收进程不能读出发送方退出前发送且未读的数据。
tcp_server.c
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <netinet/in.h> // struct sockaddr_incli_client_socket_init(void)
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int ret = 0;
int servfd = -1, len = 0, timeval = 0;
uint32_t event_flag = 0;
struct sockaddr_in servaddr, cliaddr;
if (1 == argc) {
timeval = 1000;
} else {
timeval = atoi(argv[1]);
}
if (3 == argc) {
if (strcmp(argv[2], "et") == 0) {
event_flag |= EPOLLET;
}
}
servfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == servfd) {
perror("socket");
ret = -1;
goto errexit;
}
int reuse = 1;
ret = setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, &reuse, (socklen_t ) sizeof(int));
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(4444);
//servaddr.sin_addr.s_addr = ADDR_ANY;
ret = bind(servfd,
(const struct sockaddr *) &servaddr,
(socklen_t ) sizeof(struct sockaddr_in));
if (-1 == ret) {
perror("bind");
ret = -1;
goto errexit;
}
#define LISTEN_MAX 5
ret = listen(servfd, LISTEN_MAX);
if (-1 == ret) {
perror("listen");
ret = -1;
goto errexit;
}
int epfd = -1;
struct epoll_event event;
epfd = epoll_create(LISTEN_MAX);
if (-1 == ret) {
perror("epoll_create");
ret = -1;
goto errexit;
}
memset(&event, 0, sizeof(struct epoll_event));
event.data.fd = servfd;
event.events = EPOLLIN | event_flag;
epoll_ctl(epfd, EPOLL_CTL_ADD, servfd, &event);
int nfds = 0, connfd = -1, loop = 1;
char read_buf[256];
struct epoll_event events;
socklen_t clilen = sizeof(struct sockaddr_in);
while (1) {
usleep(1000 * timeval);
nfds = epoll_wait(epfd, &events, 1, 0);
//nfds = epoll_wait(epfd, &events, 1, timeval);
if (-1 == nfds) {
printf("error occurs\n");
continue;
} else if (0 == nfds) {
// timeout
printf("timeout[%d]\n", loop++);
} else {
if (servfd == events.data.fd) {
connfd = accept(servfd, (struct sockaddr *) &cliaddr, &clilen);
if (-1 == connfd) {
perror("accept");
continue;
}
memset(&event, 0, sizeof(struct epoll_event));
event.data.fd = connfd;
event.events = EPOLLIN | event_flag;
epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &event);
printf("new connect\n");
} else {
memset(read_buf, 0, sizeof(read_buf));
read(events.data.fd, read_buf, 1);
printf("data[%s]\n", read_buf);
}
}
}
errexit:
return ret;
}
tcp_client.c
#include <sys/types.h> // socket
#include <sys/socket.h> // socket
#include <netinet/in.h> // struct sockaddr_incli_client_socket_init(void)
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int ret = 0;
int cli_fd = -1, len = 0, timeval = 0;
uint32_t serv_ip = 0;
struct sockaddr_in servaddr, cliaddr;
if (1 == argc) {
printf("Usage: ./cli IP TIME\n");
return -1;
} else if (2 == argc) {
timeval = 1000;
} else {
timeval = atoi(argv[2]);
}
cli_fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == cli_fd) {
perror("socket");
ret = -1;
goto errexit;
}
int reuse = 1;
ret = setsockopt(cli_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, (socklen_t ) sizeof(int));
int fdflag = 0;
fdflag = fcntl(cli_fd, F_GETFL);
if (fdflag & O_NONBLOCK == O_NONBLOCK) {
printf("nonblock socket.\n");
} else {
printf("block socket.\n");
}
printf("dest ip: %s\r\n", argv[1]);
ret = inet_pton(AF_INET, argv[1], &serv_ip);
if (1 != ret) {
perror("inet_pton");
ret = -1;
goto errexit;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(4444);
servaddr.sin_addr.s_addr = serv_ip;
ret = connect(cli_fd, (const struct sockaddr *) &servaddr, (socklen_t) sizeof(servaddr));
if (-1 == ret) {
perror("connect");
goto errexit;
}
printf("Succeed to connect to server\r\n");
int loop = 1;
char write_buf[256] = {"x"};
while (1) {
memset(write_buf, 0, sizeof(write_buf));
snprintf(write_buf, sizeof(write_buf), "%d", loop++);
write(cli_fd, write_buf, strlen(write_buf));
printf("%s\n", write_buf);
usleep(1000 * timeval);
}
errexit:
return ret;
}
三、阻塞/非阻塞与水平触发/边沿触发
1. tcp监听套接字list_fd最好采用水平触发方式。因为客户端高并发连接的情况下,边沿触发方式可能某些客户端连接不上服务端。
2. tcp已连接套接字conn_fd最好设置成非阻塞套接字,且以边沿触发的方式加入到epoll中;并且每次收到输入事件应该以循环的方式读取数据,直至recv()返回-1且errno被设置为EAGAIN或EWOULDBLOCK。
金沙江项目中为什么采用水平触发,为什么采用边沿触发会丢事件。