-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll_server.c
171 lines (150 loc) · 2.96 KB
/
epoll_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/epoll.h>
#define SIZE 1024
typedef struct epbuf
{
int fd;
char buf[SIZE];
}epbuf_t,*epbuf_p;
static epbuf_p alloc_epbuf(int fd)
{
epbuf_p ptr = (epbuf_p)malloc(sizeof(epbuf_t));
if (ptr == NULL)
{
perror("malloc");
exit(0);
}
ptr->fd =fd;
return ptr;
}
static void del_epbuf(epbuf_p ptr)
{
if (ptr != NULL)
{
free(ptr);
}
}
int start_up(const char * ip,int port)
{
int sock =socket(AF_INET,SOCK_STREAM,0);
if (sock <0)
{
perror("socket");
exit(1);
}
struct sockaddr_in local;
local.sin_family =AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr =inet_addr( ip);
if (bind(sock,(struct sockaddr *)&local ,sizeof(local))<0)
{
perror("bind");
exit(2);
}
if (listen(sock,5)<0)
{
perror("listen");
exit(3);
}
return sock;
}
int main(int argc,char * argv[])
{
if (argc != 3)
{
printf("Usage:%s local_ip local_port \n",argv[1]);
exit(4);
}
int epollfd =epoll_create(256);
if (epollfd < 0)
{
perror("epoll_create");
exit(5);
}
struct epoll_event ev;
struct epoll_event ready_evs[32];
int max_evs =32;
int nfds,conn_sock,listen_sock;
ev.data.ptr =alloc_epbuf(listen_sock);
ev.events =EPOLLIN|EPOLLET;
struct sockaddr_in peer;
socklen_t len =sizeof(peer);
int timeout =5000;
if (epoll_ctl(epollfd,EPOLL_CTL_ADD,listen_sock,&ev)<0)
{
perror("epoll_ctl");
exit(6);
}
nfds =epoll_wait(epollfd,ready_evs,max_evs,timeout);
if (nfds ==0)
{
printf("timeout\n");
}
else if (nfds <0)
{
perror("epoll_wait");
}
else
{
int j=0;
int fd =((epbuf_p)ready_evs[j].data.ptr)->fd;
for(;j<nfds;j++)
{
if((fd == listen_sock)&&(ready_evs[j].events & EPOLLIN))
{
conn_sock =accept(listen_sock,(struct sockaddr *)&peer,&len);
if (conn_sock <0 )
{
perror("accept");
exit(7);
}
ev.events =EPOLLIN;
ready_evs[j].data.ptr = alloc_epbuf(conn_sock);
epoll_ctl(epollfd,EPOLL_CTL_ADD,conn_sock,&ev);
}
else if (fd != listen_sock && (ready_evs[j].events &EPOLLIN))
{
char *buf = ((epbuf_p)(ready_evs[j].data.ptr))->buf;
ssize_t s =read( fd,buf ,sizeof(buf)-1);
if (s <0)
{
perror("read");
return 2;
}
else if(s>0)
{
buf[s]=0;
printf(" %s \n",buf);
ev.events =EPOLLOUT;
epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&ev);
}
else
{
del_epbuf(ready_evs[j].data.ptr);
ready_evs[j].data.ptr =NULL;
epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
close(fd);
}
}
else if(fd != listen_sock && (ready_evs[j].events & EPOLLOUT))
{
const char * smg ="HTTP/1.0 200\r\n\r\n<html><h1>hello world!</h1></html>\n";
write(fd,smg,sizeof(smg));
del_epbuf(ready_evs[j].data.ptr);
ready_evs[j].data.ptr =NULL;
epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
close(fd);
}
else
{
}
}
}
return 0;
}