-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathserver.cpp
195 lines (173 loc) · 4.03 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 <bits/stdc++.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <thread>
#include <mutex>
#define MAX_LEN 200
#define NUM_COLORS 6
using namespace std;
struct terminal
{
int id;
string name;
int socket;
thread th;
};
vector<terminal> clients;
string def_col="\033[0m";
string colors[]={"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[35m","\033[36m"};
int seed=0;
mutex cout_mtx,clients_mtx;
string color(int code);
void set_name(int id, char name[]);
void shared_print(string str, bool endLine);
int broadcast_message(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=INADDR_ANY;
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);
cout<<colors[NUM_COLORS-1]<<"\n\t ====== Welcome to the chat-room ====== "<<endl<<def_col;
while(1)
{
if((client_socket=accept(server_socket,(struct sockaddr *)&client,&len))==-1)
{
perror("accept error: ");
exit(-1);
}
seed++;
thread t(handle_client,client_socket,seed);
lock_guard<mutex> guard(clients_mtx);
clients.push_back({seed, string("Anonymous"),client_socket,(move(t))});
}
for(int i=0; i<clients.size(); i++)
{
if(clients[i].th.joinable())
clients[i].th.join();
}
close(server_socket);
return 0;
}
string color(int code)
{
return colors[code%NUM_COLORS];
}
// 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=string(name);
}
}
}
// For synchronisation of cout statements
void shared_print(string str, bool endLine=true)
{
lock_guard<mutex> guard(cout_mtx);
cout<<str;
if(endLine)
cout<<endl;
}
// Broadcast message to all clients except the sender
int broadcast_message(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);
}
}
}
// 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);
}
}
}
void end_connection(int id)
{
for(int i=0; i<clients.size(); i++)
{
if(clients[i].id==id)
{
lock_guard<mutex> guard(clients_mtx);
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
string welcome_message=string(name)+string(" has joined");
broadcast_message("#NULL",id);
broadcast_message(id,id);
broadcast_message(welcome_message,id);
shared_print(color(id)+welcome_message+def_col);
while(1)
{
int bytes_received=recv(client_socket,str,sizeof(str),0);
if(bytes_received<=0)
return;
if(strcmp(str,"#exit")==0)
{
// Display leaving message
string message=string(name)+string(" has left");
broadcast_message("#NULL",id);
broadcast_message(id,id);
broadcast_message(message,id);
shared_print(color(id)+message+def_col);
end_connection(id);
return;
}
broadcast_message(string(name),id);
broadcast_message(id,id);
broadcast_message(string(str),id);
shared_print(color(id)+name+" : "+def_col+str);
}
}