-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.cpp
207 lines (186 loc) · 5.16 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
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
using namespace std;
#define MAX_CLIENTS 20
#define BUFFER_SIZE 2048
#define NAME_LENGTH 32
#define CLIENTS_AT_A_TIME 10
//gloabl variables
int clients_fd[MAX_CLIENTS];
fd_set fdSet;
//functions
void reset_fd_set(int serverfd)
{
FD_ZERO(&fdSet);
FD_SET(serverfd, &fdSet);
for(int i = 0; i<MAX_CLIENTS; i++)
{
if(clients_fd[i] == 0)
{
continue;
}
FD_SET(clients_fd[i], &fdSet);
}
}
void trim_buffer(char *msg, int length)
{
for(int i = 0; i<length; i++)
{
if(msg[i] == '\n' || msg[i] == '\r')
{
msg[i] = '\0';
break;
}
}
}
void send_message(int clientfd, char* msg)
{
sprintf(msg, "%s\n", msg);
for(int i = 0; i<MAX_CLIENTS; i++)
{
if(clients_fd[i] == clientfd || clients_fd[i] == 0)
{
continue;
}
else
{
write(clients_fd[i], msg, strlen(msg));
}
}
}
void check(int check, char *msg)
{
if(check < 0)
{
perror(msg);
}
}
int main(int argc, char* argv[])
{
//setting up variables
int server_fd, max_fd, current_client, activity, portno, opt;
opt = 1;
char buffer[BUFFER_SIZE];
portno = atoi(argv[1]);
//setting up server properties
struct sockaddr_in address;
address.sin_port = htons(portno);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
socklen_t addrlen = sizeof(address);
//initialising client file descriptors to 0
for(int i = 0; i<MAX_CLIENTS; i++)
{
clients_fd[i] = 0;
}
//creating server socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if(server_fd == 0)
{
perror("Socket Creation Failed\n");
}
//enabling server socket to resuse address
check(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt)), "setsocket Failed\n");
//binding server
check( bind(server_fd, (struct sockaddr*)&address, addrlen) , "Bind failed\n");
//listening for new connections
check( listen(server_fd, CLIENTS_AT_A_TIME), "listen failed\n");
cout<<"Server listening on Port "<<portno<<"\n";
//clearing the socket file descriptors
FD_ZERO(&fdSet);
FD_SET(server_fd, &fdSet);
//accepting clients
while(1)
{
bzero(buffer, BUFFER_SIZE);
int sd;
max_fd = server_fd;
//setting up client fds in read fds set
for(int i = 0; i<MAX_CLIENTS; i++)
{
sd = clients_fd[i];
if(sd > 0)
{
FD_SET(sd, &fdSet);
}
//finding maximum file descriptor
if(sd > max_fd)
{
max_fd = sd;
}
}
//printf("Waiting for activity\n");
reset_fd_set(server_fd);
activity = select(max_fd + 1, &fdSet, NULL, NULL, NULL);
check(activity, "Select Failed\n");
if(activity == 0)
{
continue;
}
else if(FD_ISSET(server_fd, &fdSet))
{
sprintf(buffer, "Welcome to the Server\n");
int new_client = accept(server_fd, (struct sockaddr*)&address, &addrlen);
if(new_client == 0)
{
cout<<"Accept Failed\n";
exit(1);
}
cout<<"Client "<<new_client<<" added\n" ;
write(new_client, buffer, strlen(buffer));
//adding new client to array
for(int i = 0; i<MAX_CLIENTS; i++)
{
if(clients_fd[i] == 0)
{
clients_fd[i] = new_client;
break;
}
}
}
else
{
for(int i = 0; i<MAX_CLIENTS; i++)
{
if(clients_fd[i] == 0)
{
continue;
}
else if(FD_ISSET(clients_fd[i], &fdSet))
{
if(read(clients_fd[i], buffer, BUFFER_SIZE) == 0)
{
cout<<"client "<<clients_fd[i]<<" closing connection\n";
FD_CLR(clients_fd[i], &fdSet);
close(clients_fd[i]);
clients_fd[i] = 0;
break;
}
trim_buffer(buffer, strlen(buffer));
cout<<buffer<<endl;
if(strcmp(buffer, "/leave") == 0)
{
cout<<"client "<<clients_fd[i]<<"closing connection\n";
FD_CLR(clients_fd[i], &fdSet);
close(clients_fd[i]);
clients_fd[i] = 0;
break;
}
else
{
send_message(clients_fd[i], buffer);
bzero(buffer, BUFFER_SIZE);
break;
}
}
}
}
}
}