-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.cpp
106 lines (91 loc) · 2.43 KB
/
server.cpp
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
/*************************************************************************
> File Name: server.cpp
> Author: Ukey
> Mail: [email protected]
> Created Time: 2017年02月11日 星期六 20时08分39秒
************************************************************************/
#include "utility.h"
int main(int argc, char *argv[])
{
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr.s_addr);
int listener = socket(AF_INET, SOCK_STREAM, 0);
if(listener < 0)
{
perror("listener");
exit(-1);
}
printf("listen socket created\n");
if(bind(listener, (struct sockaddr *)&server_addr, sizeof(server_addr)))
{
perror("bind error");
exit(-1);
}
int ret = listen(listener, 20);
if(ret < 0)
{
perror("listen error");
exit(-1);
}
printf("Start to listen: %s\n", SERVER_IP);
int epfd = epoll_create(EPOLL_SIZE);
if(epfd < 0)
{
perror("epfd error");
exit(-1);
}
printf("epoll created, epollfd = %d\n", epfd);
static struct epoll_event events[EPOLL_SIZE];
addfd(epfd, listener, true);
while(1)
{
int epoll_events_count = epoll_wait(epfd, events, EPOLL_SIZE, -1);
if(epoll_events_count < 0)
{
perror("epoll failure");
break;
}
printf("epoll_events_count = %d\n", epoll_events_count);
for(int i = 0; i < epoll_events_count; i++)
{
int sockfd = events[i].data.fd;
if(sockfd == listener)
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(struct sockaddr_in);
int clientfd = accept(listener, (struct sockaddr *)&client_address, &client_addrlength);
printf("client connection from: %s : %d(IP : port), clientfd = %d \n",
inet_ntoa(client_address.sin_addr),
ntohs(client_address.sin_port),
clientfd);
addfd(epfd, clientfd, true);
clients_list.push_back(clientfd);
printf("Add new clientfd = %d to epoll\n", clientfd);
printf("Now there are %d clients in the chat room\n", (int)clients_list.size());
printf("welcome message\n");
char message[BUF_SIZE];
bzero(message, BUF_SIZE);
sprintf(message, SERVER_WELCOME, clientfd);
int ret = send(clientfd, message, BUF_SIZE, 0);
if(ret < 0)
{
perror("send error");
exit(-1);
}
}
else
{
int ret = sendBroadcastmessage(sockfd);
if(ret < 0)
{
perror("error");
exit(-1);
}
}
}
}
close(listener);
close(epfd);
}