-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpServer.cc
167 lines (132 loc) · 3.18 KB
/
TcpServer.cc
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
///
/// @file TcpServer.cc
/// @author sgzed([email protected])
/// @date 2018-03-10 23:52:28
///
#include "TcpServer.h"
#include <sys/socket.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h> //for bzero
#include <assert.h>
#include <vector>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/epoll.h>
#include <iostream>
using std::cout;
using std::endl;
using std::vector;
#define MAX_LINE 100
#define MAX_EVENTS 500
#define RESERVE_EVENTS 16
#define MAX_LISTENFD 5
bool setNonblock(int fd)
{
int flag = fcntl(fd,F_GETFL);
flag |= O_NONBLOCK;
return fcntl(fd,F_SETFL,flag)!=-1;
}
int TcpServer::createAndListen()
{
int on=1;
int listenfd = socket(AF_INET,SOCK_STREAM,0);
assert(setNonblock(listenfd)==true);
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(2018);
if(-1 == bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)))
{
cout << "bind err, errno" << errno << endl;
}
if(-1 == listen(listenfd,MAX_LISTENFD))
{
cout << "listen err, errno" << errno << endl;
exit(-1);
}
return listenfd;
}
void TcpServer::start()
{
int listenfd,connfd,sockfd;
char line[MAX_LINE];
struct sockaddr_in peeraddr;
socklen_t perrlen = sizeof perrlen;
int epollfd = epoll_create1(EPOLL_CLOEXEC);
if(epollfd<0)
{
cout << "epoll_create err,errno:" << errno << endl;
exit(-1);
}
listenfd = createAndListen();
struct epoll_event event;
vector<struct epoll_event> evs(RESERVE_EVENTS);
//evs.reserve(RESERVE_EVENTS);
event.data.fd = listenfd;
event.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,listenfd,&event);
int ret,nrecv;
while(1)
{
ret = epoll_wait(epollfd,&*evs.begin(),static_cast<int>(evs.size()),-1);
if(-1 == ret)
{
perror("epoll_wait");
exit(-1);
}
for(int i=0;i<ret;++i)
{
if(evs[i].data.fd == listenfd)
{
connfd = accept(listenfd,(sockaddr*)&peeraddr,&perrlen);
if(connfd>0)
{
cout << "new connection from ["
<< inet_ntoa(peeraddr.sin_addr)
<< ":" << ntohs(peeraddr.sin_port) << "] "
<< "accept fd:" << connfd << endl;
setNonblock(connfd);
event.events = EPOLLIN;
event.data.fd = connfd;
epoll_ctl(epollfd,EPOLL_CTL_ADD,connfd,&event);
}
else
{
cout << "accept err, errno:" << errno << endl;
}
}
else if(evs[i].events&EPOLLIN)
{
sockfd = evs[i].data.fd;
bzero(line,MAX_LINE);
if((nrecv=read(sockfd,line,sizeof line))<0)
{
if(errno ==ECONNRESET )
{
cout << "ECONNREST closed socket fd:" << sockfd << endl;
close(sockfd);
}
else
cout << "sock fd" << sockfd << "read err ,errno:" << errno << endl;
}
else if(nrecv ==0)
{
cout << "peer client closed socket fd:" << sockfd << endl;
close(sockfd);
}
else
{
cout << "receive " << nrecv << " bytes data from " << sockfd << endl;
if(write(sockfd,line,nrecv)!=nrecv)
cout << "err: not finished one time" << endl;
}
}
}
}
}