-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.c
370 lines (313 loc) · 9.54 KB
/
chat.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#define _DEFAULT_SOURCE
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include "list.h"
// Buffer to store messages, adjust size to alter message character limit
#define MAX_BUFFER_SIZE 256
// Global variables
int sock_fd;
struct sockaddr_in peer_addr;
// Lists to store messages to be sent and received
List *ToBeSent;
List *Received;
// Mutexes and condition variables for thread synchronization
pthread_mutex_t ToBeSentMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ReceivedMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t toBeSentCond = PTHREAD_COND_INITIALIZER;
pthread_cond_t receivedCond = PTHREAD_COND_INITIALIZER;
// Boolean to control the main loop
bool cont = true;
/*
Thread function to handle keyboard input.
Thread reads keyboard input, processes it, and adds it to the ToBeSent list.
*/
void *keyInputThread()
{
// Set non-blocking input for keyboard
int flags = fcntl(0, F_GETFL, 0);
fcntl(0, F_SETFL, flags | O_NONBLOCK);
char input[MAX_BUFFER_SIZE];
int size;
while (cont)
{
// Read keyboard input
size = read(0, input, sizeof(input));
if (size > 0)
{
input[size] = '\0';
// Check for the termination command
if (strcmp(input, "!\n") == 0 || strcmp(input, "!\0") == 0)
{
cont = false;
char *send = strdup(input);
// Send termination command
sendto(sock_fd, input, size, 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr));
free(send);
break;
}
// Lock the mutex, add message to the list, and signal the condition variable
pthread_mutex_lock(&ToBeSentMutex);
{
List_append(ToBeSent, input);
}
pthread_mutex_unlock(&ToBeSentMutex);
pthread_cond_signal(&toBeSentCond);
}
}
return NULL;
}
/*
Thread function to handle sending messages over UDP.
This function continuously checks the ToBeSent list and sends messages over UDP.
*/
void *UDPOutputThread()
{
while (cont)
{
pthread_mutex_lock(&ToBeSentMutex);
// Wait for a message to be available in the list
while (List_count(ToBeSent) == 0)
{
pthread_cond_wait(&toBeSentCond, &ToBeSentMutex);
}
if (List_first(ToBeSent))
{
// Get the message from the list, send it, and free the memory
char *item = List_remove(ToBeSent);
char *send = strdup(item);
sendto(sock_fd, send, strlen(send), 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr));
free(send);
}
pthread_mutex_unlock(&ToBeSentMutex);
}
return NULL;
}
/*
Thread function to handle receiving messages over UDP.
This function continuously receives messages over UDP and adds them to the Received list.
*/
void *UDPInputThread()
{
char input[MAX_BUFFER_SIZE];
int size;
while (cont)
{
// Receive a message
size = recvfrom(sock_fd, input, sizeof(input), 0, NULL, NULL);
if (size < 0)
{
perror("socket recvfrom error");
break;
}
if (size > 0)
{
input[size] = '\0';
pthread_mutex_lock(&ReceivedMutex);
char *receivedData;
receivedData = strdup(input);
// Check for the termination command
if (strcmp(receivedData, "!\n") == 0 || strcmp(receivedData, "!\0") == 0)
{
cont = false;
List_append(Received, receivedData);
pthread_cond_signal(&receivedCond);
pthread_mutex_unlock(&ReceivedMutex);
break;
}
else
{
// Add the received message to the list and signal the condition variable
List_append(Received, receivedData);
pthread_cond_signal(&receivedCond);
pthread_mutex_unlock(&ReceivedMutex);
}
}
}
return NULL;
}
/*
Thread function to handle displaying received messages on the screen.
This function continuously checks the Received list and prints messages to the screen.
*/
void *screenOutputThread()
{
while (cont)
{
pthread_mutex_lock(&ReceivedMutex);
// Wait for a message to be available in the list
while (List_count(Received) == 0)
{
pthread_cond_wait(&receivedCond, &ReceivedMutex);
}
List_first(Received);
char *print = List_remove(Received);
if (strcmp(print, "!\n") == 0 || strcmp(print, "!\0") == 0)
{
free(print);
pthread_mutex_unlock(&ReceivedMutex);
break;
}
else
{
// Display the received message on the screen
FILE *file = stdout;
printf("Received > ");
fputs(print, file);
free(print);
pthread_mutex_unlock(&ReceivedMutex);
}
}
return NULL;
}
/*
Function to free memory for list items.
This function is used as a callback by the List_free function.
*/
void pItemFreeFn(void *pItem)
{
if (pItem)
{
pItem = NULL;
}
}
/*
main initializes the program, sets up sockets, creates threads, and manages their lifecycle.
return 0 on successful execution.
*/
int main(int argc, char *argv[])
{
// Initialize lists
ToBeSent = List_create();
Received = List_create();
// Check command-line arguments
if (argc < 4)
{
printf("Please Enter: <local port> <remote machine name> <remote port>\n");
return 1;
}
// Parse local and remote ports from command-line arguments
unsigned long local_port = strtoul(argv[1], NULL, 0);
if (local_port < 1024 || local_port > 65535)
{
printf("Please enter a valid local port (1024 - 65535)\n");
return 1;
}
unsigned long remote_port = strtoul(argv[3], NULL, 0);
if (remote_port < 1024 || remote_port > 65535)
{
printf("Please enter a valid remote port (1024 - 65535)\n");
return 1;
}
// Set up address information for the remote machine
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
int ret = getaddrinfo(argv[2], NULL, &hints, &result);
if (ret != 0)
{
printf("Error - invalid remote address\n");
return (1);
}
// Set up the peer address
peer_addr.sin_family = AF_INET;
peer_addr.sin_port = htons(remote_port);
struct sockaddr_in *remote_addr = NULL;
struct addrinfo *curr = result;
// Find the appropriate address from the result
while (curr)
{
if (curr->ai_family == AF_INET && curr->ai_addr != NULL)
{
remote_addr = (struct sockaddr_in *)curr->ai_addr;
break;
}
curr = curr->ai_next;
}
// Check for a valid address
if (inet_aton(inet_ntoa(remote_addr->sin_addr), &peer_addr.sin_addr) == 0)
{
printf("Error - invalid remote address\n");
return 1;
}
// Create a socket
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
{
printf("Failed to open socket\n");
return 1;
}
// Set up the server address
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(local_port);
// Bind the socket
if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0)
{
printf("Failed to bind socket\n");
return 1;
}
// Create threads for different tasks
pthread_t k_input_thread, UDP_out, UDP_in, screen_out;
// Create and check the creation of threads
if (pthread_create(&k_input_thread, NULL, keyInputThread, NULL) != 0)
{
printf("pthread_create for keyInputThread failed");
return 1;
}
if (pthread_create(&UDP_out, NULL, UDPOutputThread, NULL) != 0)
{
printf("pthread_create for UDP_out failed");
return 1;
}
if (pthread_create(&UDP_in, NULL, UDPInputThread, NULL) != 0)
{
printf("pthread_create for UDP_in failed");
return 1;
}
if (pthread_create(&screen_out, NULL, screenOutputThread, NULL) != 0)
{
printf("screen_out for UDP_out failed");
return 1;
}
// Detach threads for independent termination
pthread_detach(UDP_out);
pthread_detach(screen_out);
pthread_detach(UDP_in);
// Wait for the termination of the key input thread and initiate termination of other threads
if (pthread_join(k_input_thread, NULL) == 0)
{
pthread_cancel(UDP_out);
usleep(10000);
pthread_cancel(screen_out);
usleep(10000);
pthread_cancel(UDP_in);
usleep(10000);
}
// Wait for the termination of the remaining threads
pthread_join(UDP_out, NULL);
pthread_join(screen_out, NULL);
pthread_join(UDP_in, NULL);
// Free address information and close the socket
freeaddrinfo(result);
close(sock_fd);
// Free memory used by lists
List_free(Received, pItemFreeFn);
List_free(ToBeSent, pItemFreeFn);
printf("\n< Chat Has Been Ended >\n");
return 0;
}