-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
186 lines (160 loc) · 4.47 KB
/
server.cpp
File metadata and controls
186 lines (160 loc) · 4.47 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
#include <bits/stdc++.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <thread>
#include <pthread.h>
#define MAX_LEN 200
using namespace std;
struct client
{
int id;
string name;
int socket;
thread th;
};
vector<client> clients;
string def_col="\033[0m";
string colors="\033[0m";
int client_connected=0;
pthread_mutex_t cout_lock = PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_t clients_lock = PTHREAD_MUTEX_INITIALIZER ;
void handle_connection(int client_socket, int id);
int check(int exp, const char *msg);
void set_name(int id, char name[]);
void print(string str);
void sendToAll(string message, int sender_id);
void sendToAll(int num, int sender_id);
void endConnection(int id);
typedef struct sockaddr_in SA_IN ;
typedef struct sockaddr SA ;
#define SERVERPORT 8989
#define SOCKETERROR (-1)
#define SERVER_BACKLOG 100
int main(){
int server_socket;
SA_IN server_addr;
//Socket Creation
check((server_socket = socket(AF_INET , SOCK_STREAM , 0)),
"Failed to create socket") ;
//Address definition
server_addr.sin_family = AF_INET ;
server_addr.sin_addr.s_addr = INADDR_ANY ;
server_addr.sin_port = htons(SERVERPORT) ;
int opt=1;
//Reuse the address and port number
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,sizeof(opt))){
cout<<"Setsocketopt error\n";
exit(1);
}
//Bind the socket with the address and port
check((bind(server_socket, (SA*)&server_addr , sizeof(server_addr))),
"Bind Failed!") ;
//Put the server in listning mode
check(listen(server_socket, SERVER_BACKLOG),
"Listen Failed!") ;
SA client;
int client_socket;
unsigned int len=sizeof(SA);
cout<<"\n\t ----- Welcome Everyone ----- "<<endl;
while(true){
//Server accepting the new client connection
check((client_socket=accept(server_socket,(struct sockaddr *)&client,&len))
,"Failed to accept client");
client_connected++;
//Thread creation
thread t(handle_connection,client_socket,client_connected);
//Mutex lock for accessing critical section
pthread_mutex_lock(&clients_lock);
clients.push_back({client_connected, string("Anonymous"),client_socket,(move(t))});
pthread_mutex_unlock(&clients_lock);
}
//Wait untill all the thread has finished execution
for(int i=0; i<clients.size(); i++){
if(clients[i].th.joinable())
clients[i].th.join();
}
//close the server
close(server_socket);
return 0;
}
//Handling the client
void handle_connection(int client_socket, int id){
char name[MAX_LEN],str[MAX_LEN];
//get the name of client
recv(client_socket,name,sizeof(name),0);
//set it
set_name(id,name);
//send welcome message to all clients except itself
string welcome_message=string(name)+string(" has joined");
sendToAll("#NULL",id);
sendToAll(id,id);
sendToAll(welcome_message,id);
print(colors+welcome_message+def_col);
while(true)
{
int size_recv=recv(client_socket,str,sizeof(str),0);
if(size_recv<=0)
return;
sendToAll(string(name),id);
sendToAll(id,id);
sendToAll(string(str),id);
print(colors+name+" : "+def_col+str);
}
}
//set the 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);
}
}
// print the endline
void print(string str){
//Mutex lock
pthread_mutex_lock(&cout_lock);
cout<<str<<endl;
pthread_mutex_unlock(&cout_lock);
}
// Send messages to all clients except itself
void sendToAll(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);
}
}
}
// Send number to all clients except itself
void sendToAll(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);
}
}
}
//End the connection
void endConnection(int id){
for(int i=0; i<clients.size(); i++){
if(clients[i].id==id){
//Mutex Lock
pthread_mutex_lock(&clients_lock);
clients[i].th.detach();
clients.erase(clients.begin()+i);
close(clients[i].socket);
pthread_mutex_unlock(&clients_lock);
break;
}
}
}
//print error and exit the code
int check(int exp, const char *msg) {
if(exp == SOCKETERROR){
perror(msg) ;
exit(1) ;
}
return exp ;
}