-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
234 lines (171 loc) · 4.8 KB
/
Copy pathmain.cpp
File metadata and controls
234 lines (171 loc) · 4.8 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <cstring>
#include <unistd.h>
#include <thread>
#include <iostream>
#include <vector>
#include <string>
#include <mutex>
#include <set>
#include <stack>
#include <iterator>
#include <poll.h>
//server
#define BUF_SIZE 256
std::mutex sockets_mutex;
std::mutex client_count_mutex;
std::set<int> sockets;
static const int num_threads = 10;
static int client_count = 0;
bool sock_flag = true;
bool authorization(int sock, std::string & client_name){
if (sock < 0){
printf("accept() failed: %d\n", errno);
return false;
}
char buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);
write(sock, "enter name\n", 12);
read(sock, buf, BUF_SIZE - 1);
client_name = std::string(buf);
printf("client %s\n", buf);
memset(buf, 0, BUF_SIZE);
write(sock, "enter password\n", 16);
read(sock, buf, BUF_SIZE - 1);
if(*buf == 'w'){
printf("client success connect\n");
client_count_mutex.lock();
client_count++;
client_count_mutex.unlock();
write(sock, "success connect\n", 16);
return true;
}else{
write(sock, "unsuccess connect\n", 18);
printf("client unsuccess connect\n");
}
return false;
}
void client_func(int newsock){
std::string client_name;
if(authorization(newsock, client_name)){
sockets_mutex.lock();
sockets.insert(newsock);
sockets_mutex.unlock();
}else{
close(newsock);
return;
}
char buf[BUF_SIZE];
if (newsock < 0){
printf("accept() failed: %d\n", errno);
}
printf("new client %s\n", client_name.c_str());
while(1){
memset(buf, 0, BUF_SIZE);
read(newsock, buf, BUF_SIZE-1);
buf[BUF_SIZE] = 0;
printf("MSG: %s\n", buf);
if(*buf == '0'){
printf("close socket \n");
sockets_mutex.lock();
auto it = sockets.find(newsock);
if(it != sockets.end())
sockets.erase(it);
sockets_mutex.unlock();
client_count_mutex.lock();
client_count--;
client_count_mutex.unlock();
close(newsock);
break;
}else{
write(newsock, "OK\n", 4);
sockets_mutex.lock();
for(auto sock : sockets)
if(sock > 0 && sock != newsock){
write(sock, client_name.c_str(), strlen(client_name.c_str()));
write(sock, buf, BUF_SIZE - 1);
}
sockets_mutex.unlock();
}
}
}
void add_sock(int sock){
std::vector<std::thread> client_threads;
struct sockaddr_in cli_addr;
int clen = sizeof(cli_addr);
listen(sock, 1);
struct timeval tv;
int newsock;
fd_set active_fd_set;
while(1)
{
printf("while");
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO (&active_fd_set);
FD_SET (sock, &active_fd_set);
if(select(FD_SETSIZE, &active_fd_set, &active_fd_set, NULL, &tv) > 0){
newsock = accept(sock, (struct sockaddr * ) &cli_addr, (socklen_t *) &clen);
}else if(sock_flag){
continue;
}else
break;
printf("\n");
if(newsock > 0)
printf("new client try connect\n");
else
break;
client_threads.push_back(std::thread(client_func, newsock));
}
for(auto & i : client_threads)
i.join();
close(sock);
}
int main(int argc, char ** argv)
{
int sock, port;
struct sockaddr_in serv_addr;
if (argc < 2){
fprintf(stderr,"usage: %s <port_number>\n", argv[0]);
return EXIT_FAILURE;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
printf("socket() failed: %d\n", errno);
return EXIT_FAILURE;
}
memset((char *) &serv_addr, 0, sizeof(serv_addr));
port = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
printf("bind() failed: %d\n", errno);
close(sock);
return EXIT_FAILURE;
}
printf("server lestening %d \n", port);
std::thread add_sock_thread(add_sock, sock);
char s[BUF_SIZE];
while(1){
memset(s, 0, BUF_SIZE - 1);
scanf("%c", s);
if(*s == 'q'){
sock_flag = false;
add_sock_thread.join();
sockets_mutex.lock();
for(auto sock : sockets)
close(sock);
sockets_mutex.unlock();
//close(sock);
break;
}else
printf("no\n");
}
}