demo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/epoll.h>
#define BUFFER_LENGTH 5
#define EPOLL_SIZE 1024
struct fds
{
int epoll_fd;
int clientfd;
};
int setNonBlocking(int sockfd)
{
if (-1 == fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK))
{
return -1;
}
return 0;
}
void epollAddFd(int epollfd, int fd, int oneshot)
{
struct epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
if (oneshot)
{
event.events |= EPOLLONESHOT;
}
setNonBlocking(fd);
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
}
void reset_oneshot(int epollfd, int fd)
{
struct epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN