#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stdio.h>
#define EPOLLEVENTS 64
#define FDSIZE 128
#define MAX_SIZE 1024
int main(int argc, char **argv){
}
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("socket error");
exit(-1);
}
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
inet_pton(AF_INET,ip,&serveraddr.sin_addr);
serveraddr.sin_port = htons(port);
if (bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))==-1)
{
perror("bind error");
exit(-1);
}
return listenfd;
}
static void do_epoll(int listenfd){
int epolldf;
struct epoll_event events[EPOLLEVENTS];
int read_cnt;
char buf[MAX_SIZE];
memset(buf,0,MAX_SIZE);
epolldf = epoll_create(FDSIZE);
add_event(epolldf,listenfd,EPOLLIN);
for (;;)
{
read_cnt = epoll_wait(epolldf,events,EPOLLEVENTS,-1);
handle_events(epolldf,events,read_cnt,listenfd,buf);
}
close(epolldf);
}
static void handle_events(int epollfd, struct epoll_event* evnets,int num,int listenfd,char* buf){
int i;
int fd;
for ( i = 0; i < num; i++)
{
fd = evnets[i].data.fd;
if ((fd==listenfd) && (evnets[i].events&EPOLLIN))
{
handle_accept(epollfd,listenfd);
}else if (evnets[i].events&EPOLLIN){
do_read(epollfd,fd,buf);
}else if (evnets[i].events&EPOLLOUT){
do_write(epollfd,fd,buf);
}
}
}
static void add_event(int epollfd, int fd,int state){
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
if (epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&ev)<0)
{
printf("add event failed! \r\n");
}
}
static void delete_event(int epollfd, int fd,int state){
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
if (epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,&ev)<0)
{
printf("delete event failed! \r\n");
}
}
static void modify_event(int epollfd, int fd,int state){
struct epoll_event ev;
ev.events = state;
ev.data.fd = fd;
if (epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev)<0)
{
printf("modify event failed! \r\n");
}
}
static void handle_accept(int epollfd, int listenfd){
int clifd;
struct sockaddr_in cliaddr;
socklen_t cliaddrlen;
clifd = accept(epollfd, (struct sockaddr*)&cliaddr, &cliaddrlen);
if (clifd == -1)
{
perror("accept error \n");
}else{
printf("accept a new client%s:%d\n",
inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port);
add_event(epollfd,clifd,EPOLLIN);
}
}
static void do_read(int epollfd,int fd, char *buf){
int nread;
nread = read(fd,buf,MAX_SIZE);
if (nread== -1)
{
perror("read error \n");
delete_event(epollfd,fd,EPOLLIN);
close(fd);
}else if (nread==0){
fprintf(stderr,"client closed\n");
delete_event(epollfd,fd,EPOLLIN);
close(fd);
}else{
modify_event(epollfd,fd,EPOLLOUT);
}
}
static void do_write(int epollfd,int fd,char* buf){
int nwrite;
nwrite = write(epollfd,buf,strlen(buf));
if (nwrite == -1)
{
perror("write error \n");
delete_event(epollfd,fd,EPOLLOUT);
close(fd);
}else
{
modify_event(epollfd,fd,EPOLLIN);
memset(buf,0,MAX_SIZE);
}
}