-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
195 lines (168 loc) · 4.62 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
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
#include "iostream"
#include <string.h>
#include <thread>
#include <mutex>
#include <signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <unistd.h>
#define MAX_LEN 200
struct terminal
{
int id;
std::string name;
int socket;
std::thread th;
};
std::vector<terminal> clients;
int seed = 0;
std::mutex cout_mutex, clients_mutex;
void set_name(int id, char name[]);
void shared_print(std::string str, bool endLine);
int broadcast_message(std::string message, int sender_id);
int broadcast_message(int num, int sender_id);
void end_connection(int id);
void handle_client(int client_socket, int id);
int main()
{
int server_socket;
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket: ");
exit(-1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(10000);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&server.sin_zero, (0));
if ((::bind(server_socket, (struct sockaddr *)&server, sizeof(struct sockaddr_in))) == -1)
{
perror("bind error: ");
exit(-1);
}
if ((listen(server_socket, 8)) == -1)
{
perror("listen error: ");
exit(-1);
}
struct sockaddr_in client;
int client_socket;
unsigned int len = sizeof(sockaddr_in);
std::cout << "\n\t ====== Hello World ====== " << std::endl
<< "";
while (1)
{
if ((client_socket = accept(server_socket, (struct sockaddr *)&client, &len)) == -1)
{
perror("accept error: ");
exit(-1);
}
seed++;
std::thread t(handle_client, client_socket, seed);
std::lock_guard<std::mutex> guard(clients_mutex);
clients.push_back({seed, std::string("New"), client_socket, (std::move(t))});
}
for (int i = 0; i < clients.size(); i++)
{
if (clients[i].th.joinable())
clients[i].th.join();
}
close(server_socket);
return 0;
}
// Set name of client
void set_name(int id, char name[])
{
for (int i = 0; i < clients.size(); i++)
{
if (clients[i].id == id)
{
clients[i].name = std::string(name);
}
}
}
// For synchronisation of cout statements
void shared_print(std::string str, bool endLine = true)
{
std::lock_guard<std::mutex> guard(cout_mutex);
std::cout << str;
if (endLine)
std::cout << std::endl;
}
// Broadcast message to all clients except the sender
int broadcast_message(std::string message, int sender_id)
{
char temp[MAX_LEN];
strcpy(temp, message.c_str());
for (int i = 0; i < clients.size(); i++)
{
if (clients[i].id != sender_id)
{
send(clients[i].socket, temp, sizeof(temp), 0);
}
}
return 0;
}
// Broadcast a number to all clients except the sender
int broadcast_message(int num, int sender_id)
{
for (int i = 0; i < clients.size(); i++)
{
if (clients[i].id != sender_id)
{
send(clients[i].socket, &num, sizeof(num), 0);
}
}
return 0;
}
void end_connection(int id)
{
for (int i = 0; i < clients.size(); i++)
{
if (clients[i].id == id)
{
std::lock_guard<std::mutex> guard(clients_mutex);
clients[i].th.detach();
clients.erase(clients.begin() + i);
close(clients[i].socket);
break;
}
}
}
void handle_client(int client_socket, int id)
{
char name[MAX_LEN], str[MAX_LEN];
recv(client_socket, name, sizeof(name), 0);
set_name(id, name);
// Display welcome message
std::string welcome_message = std::string(name) + std::string(" has joined");
broadcast_message("#NULL", id);
broadcast_message(id, id);
broadcast_message(welcome_message, id);
shared_print(welcome_message + "");
while (1)
{
ssize_t bytes_received = recv(client_socket, str, sizeof(str), 0);
if (bytes_received <= 0)
return;
if (strcmp(str, "#exit") == 0)
{
// Display leaving message
std::string message = std::string(name) + std::string(" has left");
broadcast_message("#NULL", id);
broadcast_message(id, id);
broadcast_message(message, id);
shared_print(message + "");
end_connection(id);
return;
}
broadcast_message(std::string(name), id);
broadcast_message(id, id);
broadcast_message(std::string(str), id);
shared_print(std::string(name) + ": " + "" + str);
}
}