-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepollServer.c
149 lines (126 loc) · 3.25 KB
/
epollServer.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
/*************************************************************************
> File Name: epollServer.c
> Author: lidongmeng
> Mail:lidongmeng0213@163.com
> Created Time: Fri 11 Sep 2015 05:09:02 PM PDT
************************************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>
#define MAXEPOLLSIZE 100000
#define MAXLINE 1024
int handle(int connfd);
int setnonblocking(int sockfd) {
if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) {
return -1;
}
return 0;
}
int main(int argc, char ** argv) {
int serverPort = 6888;
int listenBackLog = 1024;
int listenfd, connfd, kdpfd, nfds, nread, n, curfds, acceptCnt = 0;
struct sockaddr_in serverAddr, clientAddr;
socklen_t clientLen = sizeof(struct sockaddr_in);
struct epoll_event ev;
struct epoll_event events[MAXEPOLLSIZE];
struct rlimit rt;
char buf[MAXLINE];
rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;
if (setrlimit(RLIMIT_NOFILE, &rt) == -1) {
perror("setrlimit error");
return -1;
}
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(serverPort);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0) {
perror("socket error");
return -1;
}
if (setnonblocking(listenfd) < 0) {
perror("setnonblocking error");
}
if (bind(listenfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == -1) {
perror("bind error");
return -1;
}
if (listen(listenfd, listenBackLog) == -1) {
perror("listen error");
return -1;
}
kdpfd = epoll_create(MAXEPOLLSIZE);
ev.events = EPOLLIN|EPOLLET;
ev.data.fd = listenfd;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listenfd, &ev) < 0) {
perror("epoll_Ctl add error");
return -1;
}
curfds = 1;
while (1) {
nfds = epoll_wait(kdpfd, events, curfds, -1);
if (nfds == -1) {
perror("epoll wait");
continue;
}
for (n = 0; n < nfds; ++n) {
if (events[n].data.fd == listenfd) {
connfd = accept(listenfd, (struct sockaddr*) &clientAddr, &clientLen);
if (connfd < 0) {
perror("accept error");
continue;
}
sprintf(buf, "Accepet from %s : %d \n", inet_ntoa(clientAddr.sin_addr), clientAddr.sin_port);
printf(buf, "");
if (curfds >= MAXEPOLLSIZE) {
printf("too many connection\n");
close(connfd);
continue;
}
if (setnonblocking(connfd) < 0) {
perror("set nonblocking error");
}
ev.events = EPOLLIN|EPOLLET;
ev.data.fd = connfd;
if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, connfd, &ev) < 0) {
printf("epoll add error");
return -1;
}
curfds++;
continue;
}
if (handle(events[n].data.fd) < 0) {
epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd, &ev);
curfds--;
}
}
}
}
int handle(int connfd) {
int nread;
char buf[MAXLINE];
nread = read(connfd, buf, MAXLINE);
if (nread == 0) {
printf("client close the connection");
close(connfd);
return -1;
}
if (nread < 0) {
perror("read errror");
close(connfd);
return -1;
}
write(connfd, buf, nread);
return 0;
}