-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
347 lines (319 loc) · 11.9 KB
/
server.c
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*------------------------------------------------------------------------------------------------------------------
-- PROGRAM: server
--
-- FILE: server.c
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 22nd, 2016: Added functionality regarding people leaving and joining
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- FUNCTIONS: int main(int argc, char **argv)
-- void InitializeAddresses()
-- void Refresh()
-- void ClearUser(size_t index)
--
--
-- NOTES:
-- This application is the server application used for ChatApp. The server communicates with clients via the
-- TCP protocol from the TCP/IP protocol suite. The server utilizes select for asynhronous communication with
-- multiple clients at a time.
--
-- The server acts as an echo server. It will echo all messages received from a client to all others, excluding the
-- one who sent it. When a client connects it will broadcast that the client connected to all other clients, as well
-- as when a client disconnects. When a client connects, it will receive messages telling it exactly how many people
-- are currently connected to the program and who they are.
----------------------------------------------------------------------------------------------------------------------*/
#include "server.h"
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: main
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 22nd, 2016: Added more functionality
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- INTERFACE: int main(amount of arguments passed in
-- pointer to the arguments passed in)
--
-- RETURN: integer value in regards to a exit code
--
-- NOTES:
-- Starting point of our server application. It will first open a TCP socket connection to read from and bind it.
-- After preparing to handle the clients, it will utilize the select call to handle clients that are connecting,
-- disconnecting and wishing to echo a message to other clients.
----------------------------------------------------------------------------------------------------------------------*/
int main (int argc, char **argv) {
//Prepare all variables that will be needed
int currentNewestClient, numReadibleDescriptors, bytesToRead, socketOptionSetting = 1;
int listeningSocketDescriptor, newSocketDescriptor, curClientSocket, port, clientLatestSocket, client[FD_SETSIZE];
//Structures to hold server address and current client address
struct sockaddr_in server, clientAddress;
//Our buffer that will hold data and a pointer that will point to our buffer
char *bufPointer, buf[BUFLEN];
//Bytes read. ssize_t allows our unsigned value to also hold -1 if needed for error checking
ssize_t bytesRead;
//Holds the different checks needed for select
fd_set curSet, allSet;
//Temporary variables used when iterating through for loops
int i, j;
//Client socket length
socklen_t clientLength;
//Check if the user specified a port or not. If not, use the default.
switch(argc) {
//No port specified, use the default
case 1:
port = PORTNO;
break;
//A port specified, use that port
case 2:
port = atoi(argv[1]);
break;
//More than one argument, something is wrong. Explain usage and exit
default:
fprintf(stderr, "Usage: %s [(optional)port]\n", argv[0]);
exit(1);
}
//Create a stream socket
if ((listeningSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1)
CriticalError("Error creating the socket");
//Set the socket options ont he descriptor as needed
if (setsockopt (listeningSocketDescriptor, SOL_SOCKET, SO_REUSEADDR, &socketOptionSetting, sizeof(socketOptionSetting)) == -1)
CriticalError("Error calling setsockopt()");
//Prepare to bind the socket
bzero((char *)&server, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
//Accept all connections from all clients
server.sin_addr.s_addr = htonl(INADDR_ANY);
//Bind our address to the socket
if (bind(listeningSocketDescriptor, (struct sockaddr *)&server, sizeof(server)) == -1)
CriticalError("bind error");
//Listen for connections. Allow up to MAXCLIENTS connections
listen(listeningSocketDescriptor, MAXCLIENTS);
//Set our latest client to the listening we just created, as our first will be on this
clientLatestSocket = listeningSocketDescriptor;
//Set our latest clients index to -1, as we have no clients yet
currentNewestClient = -1;
//Default all clients in the array to -1, as they are all unused at the start and available
for (i = 0; i < FD_SETSIZE; i++)
client[i] = -1;
//Prepare for checking FD_ISSET
FD_ZERO(&allSet);
FD_SET(listeningSocketDescriptor, &allSet);
//Set all our storage of addresses and usernames to be empty
InitializeAddresses();
//Begin our forever loop listening for connections & data
while (1) {
curSet = allSet;
//Get the number of readible descriptors from select
numReadibleDescriptors = select(clientLatestSocket + 1, &curSet, NULL, NULL, NULL);
//If a new client is connected
if (FD_ISSET(listeningSocketDescriptor, &curSet)) {
clientLength = sizeof(clientAddress);
//Accept the connection
if ((newSocketDescriptor = accept(listeningSocketDescriptor, (struct sockaddr *) &clientAddress, &clientLength)) == -1) {
CriticalError("accept error");
}
//Notify that client of all existing connections
for(j = 0; j <= currentNewestClient; j++) {
char newbuf[BUFLEN];
sprintf(newbuf, "%c%s%c%s", NEWUSER, usernames[j], MESSAGEDELIMITER, addresses[j]);
write(newSocketDescriptor, newbuf, BUFLEN); // echo to client
}
//Assign our new clients index in the client array
for (i = 0; i < FD_SETSIZE; i++) {
if (client[i] < 0) {
client[i] = newSocketDescriptor; // save descriptor
break;
}
}
//If there are too many clients, exit, otherwise, store the address in our array for displaying it
if (i == FD_SETSIZE) {
printf ("Too many clients\n");
exit(1);
} else {
strcpy(addresses[i], inet_ntoa(clientAddress.sin_addr));
}
//Add new descriptor to our set
FD_SET (newSocketDescriptor, &allSet);
if (newSocketDescriptor > clientLatestSocket)
clientLatestSocket = newSocketDescriptor;
//Set our new max index in the client array
if (i > currentNewestClient)
currentNewestClient = i;
//Refresh the UI
Refresh();
//If there is no more readible descriptors, go back to select
if (--numReadibleDescriptors <= 0)
continue;
}
//Check all clients for data
for (i = 0; i <= currentNewestClient; i++) {
if ((curClientSocket = client[i]) < 0)
continue;
//If there is something to be read on our current client
if (FD_ISSET(curClientSocket, &curSet)) {
bufPointer = buf;
bytesToRead = BUFLEN;
//Read in the packet sent
while ((bytesRead = read(curClientSocket, bufPointer, bytesToRead)) > 0) {
bufPointer += bytesRead;
bytesToRead -= bytesRead;
}
//If the packet is empty then the user closed the socket. Notify everyone of the clients disconnection
if (strlen(buf) == 0 && bytesRead == 0) {
char newbuf[BUFLEN];
sprintf(newbuf, "%c%s%c", USERLEFT, inet_ntoa(clientAddress.sin_addr));
for(j = 0; j <= currentNewestClient; j++) {
if (curClientSocket != client[j]) {
write(client[j], newbuf, BUFLEN); // echo to client
}
}
close(curClientSocket);
FD_CLR(curClientSocket, &allSet);
ClearUser(i);
Refresh();
client[i] = -1;
continue;
}
//If the packet starts with the new user flag, tell all clients of the new user
if (buf[0] == NEWUSER) {
char newbuf[BUFLEN];
char token = MESSAGEDELIMITER;
sprintf(newbuf, "%s%c%s", buf, MESSAGEDELIMITER, inet_ntoa(clientAddress.sin_addr));
for(j = 0; j <= currentNewestClient; j++) {
if (curClientSocket != client[j]) {
write(client[j], newbuf, BUFLEN); // echo to client
}
}
memmove(buf, buf + 1, strlen(buf));
strcpy(usernames[i], strtok(buf, &token));
Refresh();
//Otherwise, by default, this packet is deemed a message packet and is echo'd to all clients
} else {
char newbuf[BUFLEN];
sprintf(newbuf, "%s%c%s", inet_ntoa(clientAddress.sin_addr), MESSAGEDELIMITER, buf);
sprintf(buf, newbuf);
for(j = 0; j <= currentNewestClient; j++) {
if (curClientSocket != client[j]) {
write(client[j], buf, BUFLEN); // echo to client
}
}
}
buf[0] = '\0';
if (--numReadibleDescriptors <= 0)
break;
}
}
}
return(0);
}
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: InitializeAddresses
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- INTERFACE: void InitializeAddresses()
--
-- RETURN: void
--
-- NOTES:
-- Function that simply sets all the addresses in our address array to equal an empty string
----------------------------------------------------------------------------------------------------------------------*/
void InitializeAddresses() {
size_t i;
for(i = 0; i < MAXCLIENTS; i++)
strcpy(addresses[i], "");
}
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: Refresh
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- INTERFACE: void Refresh()
--
-- RETURN: void
--
-- NOTES:
-- Function that clears the screen and displays all currently connected clients to the console
----------------------------------------------------------------------------------------------------------------------*/
void Refresh() {
size_t i;
printf("%s", CLEARSCREENANSI);
printf("###Connected Clients###\n");
for(i = 0; i < MAXCLIENTS; i++)
if (addresses[i][0] != '\0')
printf("Address: %s - Nickname: %s\n", addresses[i], usernames[i]);
}
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: ClearUser
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- INTERFACE: void ClearUser(index of the user to clear from our arrays)
--
-- RETURN: void
--
-- NOTES:
-- Function that clears a single users data from both the address array and the nickname array. Called when a user
-- disconnects
----------------------------------------------------------------------------------------------------------------------*/
void ClearUser(size_t index) {
strcpy(addresses[index], "");
strcpy(usernames[index], "");
}
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: CriticalError
--
-- DATE: March 20th, 2016
--
-- REVISIONS: March 20th, 2016: Created
-- March 23rd, 2016: Commented
--
-- DESIGNER: Carson Roscoe
--
-- PROGRAMMER: Carson Roscoe
--
-- INTERFACE: void CriticalError(error message to be printed to standard error)
--
-- RETURN: void
--
-- NOTES:
-- Function that displays an error message and then closes the program with an error code
----------------------------------------------------------------------------------------------------------------------*/
void CriticalError(char * errorMessage) {
fprintf(stderr, errorMessage);
exit(1);
}