-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
340 lines (285 loc) · 7.5 KB
/
client.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
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <netdb.h> //Definitions for network database operations
#include <netdb.h>
#define PORT 0
//Negative value of fd signifies error returned
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define RESET "\033[0m"
#define TRUE 1
typedef enum
{
CONNECT,
DISCONNECT,
SUCCESS,
GET_USERS,
SET_USERNAME,
ERROR,
LIMIT_EXCEEDED,
PUBLIC_MESSAGE,
PRIVATE_MESSAGE,
USERNAME_ERROR
} status;
typedef struct message
{
status flag;
char username[36];
char data[256];
} message;
typedef struct connection_info
{
/* data */
int socket;
struct sockaddr_in address;
char username[36];
} connection_info;
void error(const char *message)
{
//If error occures print error message & exit
perror(message);
exit(1);
}
void trim_newline(char *text)
{
int len = strlen(text) - 1;
if (text[len] == '\n')
{
text[len] = '\0';
}
}
void get_username(char *username)
{
while (TRUE)
{
printf("Enter a username: ");
fflush(stdout);
memset(username, 0, 1000);
fgets(username, 36, stdin);
trim_newline(username);
if (strlen(username) > 30)
{
puts("Username must be 36 characters or less.");
}
else
{
break;
}
}
}
void set_username(connection_info *connection)
{
message msg;
msg.flag = SET_USERNAME;
strncpy(msg.username, connection->username, 36);
if (send(connection->socket, (void *)&msg, sizeof(msg), 0) < 0)
{
error("Send failed");
}
}
void stop_client(connection_info *connection)
{
close(connection->socket);
exit(0);
}
void connect_to_server(connection_info *connection, char *address, char *port)
{
while (TRUE)
{
get_username(connection->username);
if ((connection->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("Could not create socket");
}
connection->address.sin_addr.s_addr = inet_addr(address);
connection->address.sin_family = AF_INET;
connection->address.sin_port = htons(atoi(port));
if (connect(connection->socket, (struct sockaddr *)&connection->address, sizeof(connection->address)) < 0)
{
perror("Connect failed.");
exit(1);
}
set_username(connection);
message msg;
ssize_t recv_val = recv(connection->socket, &msg, sizeof(message), 0);
if (recv_val < 0)
{
perror("recv failed");
exit(1);
}
else if (recv_val == 0)
{
close(connection->socket);
printf("The username \"%s\" is taken, please try another name.\n", connection->username);
continue;
}
break;
}
puts("Connected to server.");
puts("Type /help for usage.");
}
void handle_user_input(connection_info *connection)
{
char input[255];
fgets(input, 255, stdin);
trim_newline(input);
if (strcmp(input, "/q") == 0 || strcmp(input, "/quit") == 0)
{
stop_client(connection);
}
else if (strcmp(input, "/l") == 0 || strcmp(input, "/list") == 0)
{
message msg;
msg.flag = GET_USERS;
if (send(connection->socket, &msg, sizeof(message), 0) < 0)
{
perror("Send failed");
exit(1);
}
}
else if (strcmp(input, "/h") == 0 || strcmp(input, "/help") == 0)
{
puts("/quit or /q: Exit the program.");
puts("/help or /h: Displays help information.");
puts("/list or /l: Displays list of users in chatroom.");
puts("@<username> <message> Send a private message to given username.");
}
else if (strncmp(input, "@", 1) == 0)
{
message msg;
msg.flag = PRIVATE_MESSAGE;
char *toUsername, *chatMsg;
toUsername = strtok(input + 1, " ");
if (toUsername == NULL)
{
puts(KRED "The format for private messages is: @<username> <message>" RESET);
return;
}
if (strlen(toUsername) == 0)
{
puts(KRED "You must enter a username for a private message." RESET);
return;
}
if (strlen(toUsername) > 20)
{
puts(KRED "The username must be between 1 and 20 characters." RESET);
return;
}
chatMsg = strtok(NULL, "");
if (chatMsg == NULL)
{
puts(KRED "You must enter a message to send to the specified user." RESET);
return;
}
strncpy(msg.username, toUsername, 20);
strncpy(msg.data, chatMsg, 255);
if (send(connection->socket, &msg, sizeof(message), 0) < 0)
{
perror("Send failed");
exit(1);
}
}
else
{
message msg;
msg.flag = PUBLIC_MESSAGE;
strncpy(msg.username, connection->username, 20);
if (strlen(input) == 0)
{
return;
}
strncpy(msg.data, input, 255);
if (send(connection->socket, &msg, sizeof(message), 0) < 0)
{
perror("Send failed");
exit(1);
}
}
}
void handle_server_message(connection_info *connection)
{
message msg;
ssize_t recv_val = recv(connection->socket, &msg, sizeof(message), 0);
if (recv_val < 0)
{
perror("recv failed");
exit(1);
}
else if (recv_val == 0)
{
close(connection->socket);
puts("Server disconnected.");
exit(0);
}
switch (msg.flag)
{
case CONNECT:
printf(KYEL "%s has connected." RESET "\n", msg.username);
break;
case DISCONNECT:
printf(KYEL "%s has disconnected." RESET "\n", msg.username);
break;
case GET_USERS:
printf(KMAG "%s" RESET "\n", msg.data);
break;
case PUBLIC_MESSAGE:
printf(KGRN "%s" RESET ": %s\n", msg.username, msg.data);
break;
case PRIVATE_MESSAGE:
printf(KWHT "From %s:" KCYN " %s\n" RESET, msg.username, msg.data);
break;
case LIMIT_EXCEEDED:
fprintf(stderr, KRED "Server chatroom is too full to accept new clients." RESET "\n");
exit(0);
break;
default:
fprintf(stderr, KRED "Unknown message type received." RESET "\n");
break;
}
}
int main(int argc, char *argv[])
{
connection_info connection;
fd_set file_descriptors;
if (argc != 3)
{
fprintf(stderr, "Usage: %s <IP> <port>\n", argv[0]);
exit(1);
}
connect_to_server(&connection, argv[1], argv[2]);
while (TRUE)
{
FD_ZERO(&file_descriptors);
FD_SET(STDIN_FILENO, &file_descriptors);
FD_SET(connection.socket, &file_descriptors);
fflush(stdin);
if (select(connection.socket + 1, &file_descriptors, NULL, NULL, NULL) < 0)
{
perror("Select failed.");
exit(1);
}
if (FD_ISSET(STDIN_FILENO, &file_descriptors))
{
handle_user_input(&connection);
}
if (FD_ISSET(connection.socket, &file_descriptors))
{
handle_server_message(&connection);
}
}
close(connection.socket);
return 0;
}