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
|
//EPOLL模型测试
#include "wrap.h"
#include <sys/epoll.h>
#include <ctype.h>
int main()
{
int ret;
int n;
int i;
int k;
int nready;
int lfd;
int cfd;
int sockfd;
char buf[1024];
socklen_t socklen;
struct sockaddr_in svraddr;
struct epoll_event ev;
struct epoll_event events[1024];
//创建socket
lfd = Socket(AF_INET, SOCK_STREAM, 0);
//设置文件描述符为端口复用
int opt = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
//绑定bind
svraddr.sin_family = AF_INET;
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
svraddr.sin_port = htons(8888);
Bind(lfd, (struct sockaddr *)&svraddr, sizeof(struct sockaddr_in));
//监听listen
Listen(lfd, 128);
//创建一棵epoll树
int epfd = epoll_create(1024);
if(epfd<0)
{
perror("create epoll error");
return -1;
}
//将lfd上epoll树
ev.data.fd = lfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);
while(1)
{
nready = epoll_wait(epfd, events, 1024, -1);
if(nready<0)
{
perror("epoll_wait error");
if(errno==EINTR)
{
continue;
}
break;
}
for(i=0; i<nready; i++)
{
//有客户端连接请求
sockfd = events[i].data.fd;
if(sockfd==lfd)
{
cfd = Accept(lfd, NULL, NULL);
//将新的cfd上epoll树
ev.data.fd = cfd;
ev.events = EPOLLIN;
epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);
continue;
}
//有客户端发送数据过来
memset(buf, 0x00, sizeof(buf));
//n = Read(sockfd, buf, sizeof(buf));
n = recv(sockfd, buf, sizeof(buf), 0);
if(n<=0)
{
printf("n==[%d], buf==[%s]\n", n, buf);
close(sockfd);
//将sockfd对应的事件就节点从epoll树上删除
epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
}
else
{
printf("n==[%d], buf==[%s]\n", n, buf);
for(k=0; k<n; k++)
{
buf[k] = toupper(buf[k]);
}
//Write(sockfd, buf, n);
send(sockfd, buf, n, 0);
}
}
}
close(epfd);
close(lfd);
return 0;
}
|