-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.c
More file actions
352 lines (308 loc) · 9.69 KB
/
server.c
File metadata and controls
352 lines (308 loc) · 9.69 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
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
348
349
350
351
352
//Eugene Li - Multithreaded chat server
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <pthread.h>
#define MAX_BUFFER 1024
/*
Queue implementation using a char array.
Contains a mutex for functions to lock on before modifying the array,
and condition variables for when it's not empty or full.
*/
typedef struct {
char *buffer[MAX_BUFFER];
int head, tail;
int full, empty;
pthread_mutex_t *mutex;
pthread_cond_t *notFull, *notEmpty;
} queue;
/*
Struct containing important data for the server to work.
Namely the list of client sockets, that list's mutex,
the server's socket for new connections, and the message queue
*/
typedef struct {
fd_set serverReadFds;
int socketFd;
int clientSockets[MAX_BUFFER];
int numClients;
pthread_mutex_t *clientListMutex;
queue *queue;
} chatDataVars;
/*
Simple struct to hold the chatDataVars and the new client's socket fd.
Used only in the client handler thread.
*/
typedef struct {
chatDataVars *data;
int clientSocketFd;
} clientHandlerVars;
void startChat(int socketFd);
void buildMessage(char *result, char *name, char *msg);
void bindSocket(struct sockaddr_in *serverAddr, int socketFd, long port);
void removeClient(chatDataVars *data, int clientSocketFd);
void *newClientHandler(void *data);
void *clientHandler(void *chv);
void *messageHandler(void *data);
void queueDestroy(queue *q);
queue* queueInit(void);
void queuePush(queue *q, char* msg);
char* queuePop(queue *q);
int main(int argc, char *argv[])
{
struct sockaddr_in serverAddr;
long port = 9999;
int socketFd;
if(argc == 2) port = strtol(argv[1], NULL, 0);
if((socketFd = socket(AF_INET, SOCK_STREAM, 0))== -1)
{
perror("Socket creation failed");
exit(1);
}
bindSocket(&serverAddr, socketFd, port);
if(listen(socketFd, 1) == -1)
{
perror("listen failed: ");
exit(1);
}
startChat(socketFd);
close(socketFd);
}
//Spawns the new client handler thread and message consumer thread
void startChat(int socketFd)
{
chatDataVars data;
data.numClients = 0;
data.socketFd = socketFd;
data.queue = queueInit();
data.clientListMutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(data.clientListMutex, NULL);
//Start thread to handle new client connections
pthread_t connectionThread;
if((pthread_create(&connectionThread, NULL, (void *)&newClientHandler, (void *)&data)) == 0)
{
fprintf(stderr, "Connection handler started\n");
}
FD_ZERO(&(data.serverReadFds));
FD_SET(socketFd, &(data.serverReadFds));
//Start thread to handle messages received
pthread_t messagesThread;
if((pthread_create(&messagesThread, NULL, (void *)&messageHandler, (void *)&data)) == 0)
{
fprintf(stderr, "Message handler started\n");
}
pthread_join(connectionThread, NULL);
pthread_join(messagesThread, NULL);
queueDestroy(data.queue);
pthread_mutex_destroy(data.clientListMutex);
free(data.clientListMutex);
}
//Initializes queue
queue* queueInit(void)
{
queue *q = (queue *)malloc(sizeof(queue));
if(q == NULL)
{
perror("Couldn't allocate anymore memory!");
exit(EXIT_FAILURE);
}
q->empty = 1;
q->full = q->head = q->tail = 0;
q->mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
if(q->mutex == NULL)
{
perror("Couldn't allocate anymore memory!");
exit(EXIT_FAILURE);
}
pthread_mutex_init(q->mutex, NULL);
q->notFull = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
if(q->notFull == NULL)
{
perror("Couldn't allocate anymore memory!");
exit(EXIT_FAILURE);
}
pthread_cond_init(q->notFull, NULL);
q->notEmpty = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
if(q->notEmpty == NULL)
{
perror("Couldn't allocate anymore memory!");
exit(EXIT_FAILURE);
}
pthread_cond_init(q->notEmpty, NULL);
return q;
}
//Frees a queue
void queueDestroy(queue *q)
{
pthread_mutex_destroy(q->mutex);
pthread_cond_destroy(q->notFull);
pthread_cond_destroy(q->notEmpty);
free(q->mutex);
free(q->notFull);
free(q->notEmpty);
free(q);
}
//Push to end of queue
void queuePush(queue *q, char* msg)
{
q->buffer[q->tail] = msg;
q->tail++;
if(q->tail == MAX_BUFFER)
q->tail = 0;
if(q->tail == q->head)
q->full = 1;
q->empty = 0;
}
//Pop front of queue
char* queuePop(queue *q)
{
char* msg = q->buffer[q->head];
q->head++;
if(q->head == MAX_BUFFER)
q->head = 0;
if(q->head == q->tail)
q->empty = 1;
q->full = 0;
return msg;
}
//Sets up and binds the socket
void bindSocket(struct sockaddr_in *serverAddr, int socketFd, long port)
{
memset(serverAddr, 0, sizeof(*serverAddr));
serverAddr->sin_family = AF_INET;
serverAddr->sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr->sin_port = htons(port);
if(bind(socketFd, (struct sockaddr *)serverAddr, sizeof(struct sockaddr_in)) == -1)
{
perror("Socket bind failed: ");
exit(1);
}
}
//Removes the socket from the list of active client sockets and closes it
void removeClient(chatDataVars *data, int clientSocketFd)
{
pthread_mutex_lock(data->clientListMutex);
for(int i = 0; i < MAX_BUFFER; i++)
{
if(data->clientSockets[i] == clientSocketFd)
{
data->clientSockets[i] = 0;
close(clientSocketFd);
data->numClients--;
i = MAX_BUFFER;
}
}
pthread_mutex_unlock(data->clientListMutex);
}
//Thread to handle new connections. Adds client's fd to list of client fds and spawns a new clientHandler thread for it
void *newClientHandler(void *data)
{
chatDataVars *chatData = (chatDataVars *) data;
while(1)
{
int clientSocketFd = accept(chatData->socketFd, NULL, NULL);
if(clientSocketFd > 0)
{
fprintf(stderr, "Server accepted new client. Socket: %d\n", clientSocketFd);
//Obtain lock on clients list and add new client in
pthread_mutex_lock(chatData->clientListMutex);
if(chatData->numClients < MAX_BUFFER)
{
//Add new client to list
for(int i = 0; i < MAX_BUFFER; i++)
{
if(!FD_ISSET(chatData->clientSockets[i], &(chatData->serverReadFds)))
{
chatData->clientSockets[i] = clientSocketFd;
i = MAX_BUFFER;
}
}
FD_SET(clientSocketFd, &(chatData->serverReadFds));
//Spawn new thread to handle client's messages
clientHandlerVars chv;
chv.clientSocketFd = clientSocketFd;
chv.data = chatData;
pthread_t clientThread;
if((pthread_create(&clientThread, NULL, (void *)&clientHandler, (void *)&chv)) == 0)
{
chatData->numClients++;
fprintf(stderr, "Client has joined chat. Socket: %d\n", clientSocketFd);
}
else
close(clientSocketFd);
}
pthread_mutex_unlock(chatData->clientListMutex);
}
}
}
//The "producer" -- Listens for messages from client to add to message queue
void *clientHandler(void *chv)
{
clientHandlerVars *vars = (clientHandlerVars *)chv;
chatDataVars *data = (chatDataVars *)vars->data;
queue *q = data->queue;
int clientSocketFd = vars->clientSocketFd;
char msgBuffer[MAX_BUFFER];
while(1)
{
int numBytesRead = read(clientSocketFd, msgBuffer, MAX_BUFFER - 1);
msgBuffer[numBytesRead] = '\0';
//If the client sent /exit\n, remove them from the client list and close their socket
if(strcmp(msgBuffer, "/exit\n") == 0)
{
fprintf(stderr, "Client on socket %d has disconnected.\n", clientSocketFd);
removeClient(data, clientSocketFd);
return NULL;
}
else
{
//Wait for queue to not be full before pushing message
while(q->full)
{
pthread_cond_wait(q->notFull, q->mutex);
}
//Obtain lock, push message to queue, unlock, set condition variable
pthread_mutex_lock(q->mutex);
fprintf(stderr, "Pushing message to queue: %s\n", msgBuffer);
queuePush(q, msgBuffer);
pthread_mutex_unlock(q->mutex);
pthread_cond_signal(q->notEmpty);
}
}
}
//The "consumer" -- waits for the queue to have messages then takes them out and broadcasts to clients
void *messageHandler(void *data)
{
chatDataVars *chatData = (chatDataVars *)data;
queue *q = chatData->queue;
int *clientSockets = chatData->clientSockets;
while(1)
{
//Obtain lock and pop message from queue when not empty
pthread_mutex_lock(q->mutex);
while(q->empty)
{
pthread_cond_wait(q->notEmpty, q->mutex);
}
char* msg = queuePop(q);
pthread_mutex_unlock(q->mutex);
pthread_cond_signal(q->notFull);
//Broadcast message to all connected clients
fprintf(stderr, "Broadcasting message: %s\n", msg);
for(int i = 0; i < chatData->numClients; i++)
{
int socket = clientSockets[i];
if(socket != 0 && write(socket, msg, MAX_BUFFER - 1) == -1)
perror("Socket write failed: ");
}
}
}