Repository navigation
Expand file tree
/
Copy pathserver.cpp
More file actions
130 lines (117 loc) · 2.74 KB
/
Copy pathserver.cpp
File metadata and controls
130 lines (117 loc) · 2.74 KB
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
#include "config.h"
int main()
{
sockaddr_in serveraddr, clientaddr;
int sockFd, epfd, nfds;
epoll_event ev, events[CONNECT_SIZE];
socklen_t clilen;
printf("Start server.\n");
if((sockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket error");
exit(1);
}
printf("Create Socket.\n");
if((epfd = epoll_create(CONNECT_SIZE)) < 0)
{
perror("epoll_create error");
close(sockFd);
exit(1);
}
printf("Create epoll.\n");
ev.data.fd = sockFd;
ev.events = EPOLLIN | EPOLLET;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, sockFd, &ev) < 0)
{
perror("epoll_ctl error");
exit(1);
}
bzero(&serveraddr, sizeof(serveraddr));
bzero(&clientaddr, sizeof(clientaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if(bind(sockFd, (sockaddr *)&serveraddr, sizeof(sockaddr)) < 0)
{
perror("bind error");
exit(1);
}
printf("Bind Port:%d\n", PORT);
if(listen(sockFd, LISTENQ) < 0)
{
perror("listen error");
exit(1);
}
printf("Listen.\n");
while(1)
{
if((nfds = epoll_wait(epfd, events, CONNECT_SIZE, -1)) < 0)
{
perror("epoll_wait error");
continue;
}
for(int i=0;i<nfds;++i)
{
if(events[i].data.fd == sockFd)
{
int newClientFd;
clilen = sizeof(clientaddr);
if((newClientFd = accept(sockFd, (sockaddr* )&clientaddr, &clilen)) < 0)
{
perror("accept error");
continue;
}
printf("New Client [%s:%d] connected.\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
ev.data.fd = newClientFd;
ev.events = EPOLLIN | EPOLLET;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, newClientFd, &ev) < 0)
{
perror("epoll_ctl error");
continue;
}
}
else
{
char buff[MAX_LINE];
ssize_t buffLen;
if(events[i].events & EPOLLIN)
{
if((buffLen = read(events[i].data.fd, buff, MAX_LINE)) < 0)
{
perror("read error");
continue;
}
else if(buffLen == 0)
{
printf("Connect closed from client[%s:%d].\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port);
close(events[i].data.fd);
events[i].data.fd = -1;
continue;
}
clilen = sizeof(clientaddr);
bzero(&clientaddr, clilen);
if(getpeername(events[i].data.fd, (sockaddr *)&clientaddr, &clilen) < 0)
{
perror("getpeername error");
printf("Read :%s", buff);
}
else
{
printf("Read from [%s:%d]:%s", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, buff);
}
printf("Send back.\n");
if(write(events[i].data.fd, buff, buffLen) != buffLen)
{
perror("Write error");
close(events[i].data.fd);
events[i].data.fd = -1;
break;
}
}
}
}
}
free(events);
close(epfd);
exit(0);
}