#include
int epoll_create(int size);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);
EPOLL_CTL_ADD:注册新的fd到epfd中;
EPOLL_CTL_MOD:修改已经注册的fd的监听事件;
EPOLL_CTL_DEL:从epfd中删除一个fd;
struct epoll_event {
__uint32_t events; /* Epoll events */
epoll_data_t data; /* User data variable */
};
ET模式在很大程度上减少了epoll事件被重复触发的次数,因此效率要比LT模式高。epoll工作在ET模式的时候,必须使用非阻塞套接口,以避免由于一个文件句柄的阻塞读/阻塞写操作把处理多个文件描述符的任务饿死。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IPADDRESS "127.0.0.1"
#define PORT 8888
#define MAXSIZE 1024
#define LISTENQ 5
#define FDSIZE 1000
#define EPOLLEVENTS 100
//bind socket
static int socket_bind(const char* ip, int port);
//io epoll
static void do_epoll(int listenfd);
//deal with events
static void handle_events(int epollfd, struct epoll_event *event, int num, int listenfd, char *buf);
//deal with accpet events
static void handle_accpet(int epolled, int listenfd);
//deal with read
static void do_read(int epollfd, int fd, char *buf);
//deal with write
static void do_write(int epollfd, int fd, char *buf);
//change event
static void change_event(int epolled, int fd, int state, int op);
int main()
{
printf("EPOLL_CTL_ADD is %d\n", EPOLL_CTL_ADD);
printf("EPOLL_CTL_DEL is %d\n", EPOLL_CTL_DEL);
printf("EPOLL_CTL_MOD is %d\n", EPOLL_CTL_MOD);
int listenfd;
listenfd = socket_bind(IPADDRESS, PORT);
listen(listenfd, LISTENQ);
do_epoll(listenfd);
puts("epoll server end");
return 0;
}
//bind socket
static int socket_bind(const char* ip, int port)
{
int listenfd;
struct sockaddr_in serveraddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{
perror("create socket failed\n");
exit(EXIT_FAILURE);
}
//please do not forget bzero serveraddr
bzero(&serveraddr, sizeof(serveraddr));
//init serveraddr
serveraddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &serveraddr.sin_addr);
//please do not forget this is htons(port). not is port
serveraddr.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) == -1)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
return listenfd;
}
//io epoll
static void do_epoll(int listenfd)
{
int epollfd;
struct epoll_event events[EPOLLEVENTS];
int result;
char buf[MAXSIZE];
memset(buf, 0, MAXSIZE);
//create epollfd
epollfd = epoll_create(FDSIZE);
//add listen listenfd event
change_event(epollfd, listenfd, EPOLLIN, EPOLL_CTL_ADD);
while (1)
{
//get has prepared fd events
result = epoll_wait(epollfd, events, EPOLLEVENTS, -1);
printf("need deal with events is %d\n", result);
handle_events(epollfd, events, result, listenfd, buf);
}
close(epollfd);
}
//deal with events
static void handle_events(int epollfd, struct epoll_event *events, int num, int listenfd, char *buf)
{
int i;
int fd;
//foreach
for (i = 0; i < num; ++i)
{
fd = events[i].data.fd;
//deal with events by fd
if ((fd == listenfd) && (events[i].events & EPOLLIN))
{
puts("has go in handle_accpet");
handle_accpet(epollfd, listenfd);
}
else if (events[i].events & EPOLLIN)
{
puts("do read");
do_read(epollfd, fd, buf);
}
else if (events[i].events & EPOLLOUT)
{
puts("do write");
do_write(epollfd, fd, buf);
}
}
}
//deal with accpet events
static void handle_accpet(int epolled, int listenfd)
{
int clifd;
struct sockaddr_in cli_addr;
socklen_t length = sizeof(cli_addr);
clifd = accept(listenfd, (struct sockaddr*)&cli_addr, &length);
// if (clifd < 0) {
if (clifd == -1)
perror("accept failed\n");
// exit(EXIT_FAILURE);
// }
else
{
printf("accpet a new client:ip is %s and portsi %d\n", inet_ntoa(cli_addr.sin_addr), cli_addr.sin_port);
//add client event
change_event(epolled, clifd, EPOLLIN, EPOLL_CTL_ADD);
}
}
//deal with read
static void do_read(int epollfd, int fd, char *buf)
{
int nread;
nread = read(fd, buf, MAXSIZE);
if (nread == -1)
{
perror("read data failed\n");
close(fd);
//delelet event
change_event(epollfd, fd, EPOLLIN, EPOLL_CTL_DEL);
}
else if (nread == 0)
{
fprintf(stderr, "client close\n");
close(fd);
//delete event
change_event(epollfd, fd, EPOLLIN, EPOLL_CTL_DEL);
}
else
{
printf("read message is %s\n", buf);
//change event. from read to write
change_event(epollfd, fd, EPOLLOUT, EPOLL_CTL_MOD);
}
}
//deal with write
static void do_write(int epollfd, int fd, char *buf)
{
int nwrite;
nwrite = write(fd, buf, strlen(buf));
if (nwrite == -1)
{
perror("write falied\n");
close(fd);
change_event(epollfd, fd, EPOLLOUT, EPOLL_CTL_DEL);
}
else
{
change_event(epollfd, fd, EPOLLIN, EPOLL_CTL_MOD);
memset(buf, 0, MAXSIZE);
}
}
//change event
static void change_event(int epollfd, int fd, int state, int op)
{
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
epoll_ctl(epollfd, op, fd, &ev);
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSIZE 1024
#define IPADDRESS "127.0.0.1"
#define SERV_PORT 8888
#define FDSIZE 1024
#define EPOLLEVENTS 20
static void handle_connection(int sockfd);
static void handle_events(int epollfd, struct epoll_event *events, int num, int sockfd, char *buf);
static void do_read(int epollfd, int fd, int sockfd, char *buf);
static void do_write(int epollfd, int fd, int sockfd, char *buf);
static void change_event(int epollfd, int fd, int state, int op);
int main()
{
int sockfd;
struct sockaddr_in server_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("client create socket failed\n");
exit(EXIT_SUCCESS);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, IPADDRESS, &server_addr.sin_addr);
socklen_t length = sizeof(server_addr);
if (connect(sockfd, (struct sockaddr*)&server_addr, length) < 0)
{
perror("clint connect failed\n");
exit(EXIT_SUCCESS);
}
//deal with connection
handle_connection(sockfd);
//do not forget to close sockfd
close(sockfd);
return 0;
}
static void handle_connection(int sockfd)
{
int epollfd;
struct epoll_event events[EPOLLEVENTS];
char buf[MAXSIZE];
memset(buf, 0, sizeof(buf));
int result;
epollfd = epoll_create(FDSIZE);
change_event(epollfd, STDIN_FILENO,EPOLLIN, EPOLL_CTL_ADD);
while (1)
{
result = epoll_wait(epollfd, events, EPOLLEVENTS, -1);
printf("clinet need deal events is %d\n", result);
handle_events(epollfd, events, result, sockfd, buf);
}
//do not forget to close epollfd
close(epollfd);
}
static void handle_events(int epollfd, struct epoll_event *events, int num, int sockfd, char *buf)
{
int fd;
int i;
for (i = 0; i < num; i++)
{
fd = events[i].data.fd;
if (events[i].events & EPOLLIN)
{
do_read(epollfd, fd, sockfd, buf);
}
else if (events[i].events & EPOLLOUT)
{
do_write(epollfd, fd, sockfd, buf);
}
}
}
static void do_read(int epollfd, int fd, int sockfd, char *buf)
{
int nread;
nread = read(fd, buf, MAXSIZE);
if (nread == -1)
{
perror("client read fail\n");
close(fd);
}
else if (nread == 0)
{
fprintf(stderr, "server close\n");
close(fd);
}
else
{
if (fd == STDIN_FILENO)
{
puts("do read fd == STDIN_FILENO");
change_event(epollfd, sockfd, EPOLLOUT, EPOLL_CTL_ADD);
}
else
{
puts("do read fd != STDIN_FILENO");
change_event(epollfd, sockfd, EPOLLIN, EPOLL_CTL_DEL);
change_event(epollfd, STDOUT_FILENO, EPOLLOUT, EPOLL_CTL_ADD);
}
}
}
static void do_write(int epollfd, int fd, int sockfd, char *buf)
{
int nwrite;
nwrite = write(fd, buf, strlen(buf));
if (nwrite == -1)
{
perror("client write failed\n");
close(fd);
}
else
{
if (fd == STDOUT_FILENO)
{
puts("do write fd == STDOUT_FILENO");
change_event(epollfd, fd, EPOLLOUT, EPOLL_CTL_DEL);
}
else
{
puts("do write fd != STDOUT_FILENO");
change_event(epollfd, fd, EPOLLIN, EPOLL_CTL_MOD);
}
}
memset(buf, 0, MAXSIZE);
}
static void change_event(int epollfd, int fd, int state, int op)
{
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
epoll_ctl(epollfd, op, fd, &ev);
}