-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll_test.c
146 lines (125 loc) · 3.95 KB
/
epoll_test.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
//测试epoll的ET模式
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
//#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
//#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#define MAXEVENTS 200
#define BUFSIZE 50
int set_nonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return old_option;
}
int main(int argc, char * argv[])
{
if (argc <= 2)
{
printf("usage: %s ip_address port_number\n",
argv[0]);
return -1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
struct sockaddr_in sin;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
inet_pton(AF_INET, ip, &sin.sin_addr);
sin.sin_port = htons(port);
//创建并绑定socket
int sock, ret;
int epfd;
struct epoll_event event;
struct epoll_event *events;
sock = socket(PF_INET, SOCK_STREAM, 0);
assert (sock >= 0);
ret = bind(sock, (struct sockaddr*)&sin, sizeof(sin));
assert(ret != -1);
ret = listen(sock, 5);
assert(ret != -1);
//set non-blocking
ret = set_nonblocking(sock);
assert(ret != -1);
epfd = epoll_create(MAXEVENTS);
assert (epfd != -1);
event.data.fd = sock;
event.events = EPOLLIN | EPOLLET;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &event);
assert(ret != -1);
events = (struct epoll_event*)malloc(sizeof(struct epoll_event)
* MAXEVENTS);
assert(events != NULL);
while (1)
{
int n, i;
n = epoll_wait(epfd, events, MAXEVENTS, -1);
for (i = 0; i < n; ++i)
{
if ((events[i].events & EPOLLERR)
|| (events[i].events & EPOLLHUP)
|| !(events[i].events & EPOLLIN))
{
fprintf(stderr, "epoll error\n");
close(events[i].data.fd);
continue;
}
else if (sock == events[i].data.fd)
{
//struct sockadd_in cli;
//socklen_t cli_len;
int connfd, res;
//cli_len = sizeof(struct sockadd_in);
struct sockaddr_in client;
socklen_t cli_len = sizeof(client);
connfd = accept(sock, (struct sockaddr*)&client, &cli_len);
if (connfd == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
break;
}
else
{
perror("accept error");
break;
}
}
char host[BUFSIZE], serv[BUFSIZE];
res = getnameinfo((struct sockaddr*)&client, cli_len,
host, sizeof(host),
serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (res ==0)
printf("Accepted connection on descriptor %d "
"(host=%s, port=%s)\n", connfd, host, serv);
res = set_nonblocking(connfd);
assert(res != -1);
event.data.fd = connfd;
event.events = EPOLLIN | EPOLLET;
res = epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &event);
assert(res != -1);
}
else {
ssize_t count;
char buf[BUFSIZE + 1];
//buf[BUFSIZE] = '\0';
memset(buf, '\0', BUFSIZE + 1);
while ((count = read(events[i].data.fd, buf, BUFSIZE)) > 0)
{
printf("Data from client:%s", buf);
memset(buf, '\0', BUFSIZE + 1);
}
}
}
}
return 0;
}