forked from jamwt/libtask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
220 lines (174 loc) · 5.17 KB
/
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define MAXLINE 512
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 843
#define INFTIM 1000
void setnonblocking(int sock)
{
int opts;
opts=fcntl(sock,F_GETFL);
if(opts<0)
{
perror("fcntl(sock,GETFL)");
exit(1);
}
opts = opts | O_NONBLOCK;
if(fcntl(sock,F_SETFL,opts)<0)
{
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
}
ssize_t socket_send(int sockfd, const char* buffer, size_t buflen)
{
ssize_t tmp;
size_t total = buflen;
const char *p = buffer;
while(1)
{
tmp = send(sockfd, p, total, 0);
if(tmp < 0)
{
if(errno == EINTR)
//return -1;zxd
continue;
if(errno == EAGAIN)
{
usleep(1000);
continue;
}
return -1;
}
if((size_t)tmp == total)
return buflen;
total -= tmp;
p += tmp;
}
return tmp;
}
void epoll_ctl_err_show()
{
std::cout << "error at epoll_ctl" << std::endl;
if(EBADF == errno)
{
std::cout << "error at epoll_ctl, error is EBADF" << std::endl;
}
else if(EEXIST == errno)
{
std::cout << "error at epoll_ctl, error is EEXIST" << std::endl;
}
else if(EINVAL == errno)
{
std::cout << "error at epoll_ctl, error is EINVAL" << std::endl;
}
else if(ENOENT == errno)
{
std::cout << "error at epoll_ctl, error is ENOENT" << std::endl;
}
else if(ENOMEM == errno)
{
std::cout << "error at epoll_ctl, error is ENOMEM" << std::endl;
}
else if(ENOSPC == errno)
{
std::cout << "error at epoll_ctl, error is ENOSPC" << std::endl;
}
}
int main()
{
int i, maxi, listenfd, connfd, sockfd, epfd, nfds;
ssize_t n;
char line[MAXLINE];
socklen_t clilen;
struct epoll_event ev,events[20000];
epfd=epoll_create(20000);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
setnonblocking(listenfd);
ev.data.fd=listenfd;
epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
char *local_addr=(char*)"127.0.0.1";
inet_aton(local_addr,&(serveraddr.sin_addr));
serveraddr.sin_port=htons(SERV_PORT);
bind(listenfd,(sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, LISTENQ);
maxi = 0;
for( ; ; ) {
nfds=epoll_wait(epfd, events, 20, -1);
for(i=0;i<nfds;++i)
{
if(events[i].data.fd==listenfd)
{
while((connfd = accept(listenfd,(sockaddr *)&clientaddr, &clilen)) > 0)
{
setnonblocking(connfd);
char *str = inet_ntoa(clientaddr.sin_addr);
// std::cout<<"connect from "<<str<<std::endl;
ev.data.fd=connfd;
epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
}
}
else if(events[i].events&EPOLLIN)
{
if ( (sockfd = events[i].data.fd) < 0)
{
continue;
}
memset(line, 0, MAXLINE);
n = 0;
int nread = 0;
while((nread= read(sockfd, line + n, MAXLINE)) > 0)
{
n += nread;
}
if(nread == -1 && errno != EAGAIN)
{
epoll_ctl_err_show();
std::cout<<"readline error"<<std::endl;
close(sockfd);
events[i].data.fd = -1;
}
if(nread == 0)
{
close(sockfd);
continue;
}
static unsigned int count = 0;
count++;
if(count%10 == 0){
//
}else{
write(sockfd,line,n);
}
ev.data.fd=sockfd;
epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
else if(events[i].events & EPOLLOUT)
{
sockfd = events[i].data.fd;
int iRet = socket_send(sockfd, line, strlen(line) + 1);
if(iRet == -1 || iRet != strlen(line) + 1)
{
perror("write error!");
}/*zxd*/
ev.data.fd=sockfd;
epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
}
}
}
}