-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
311 lines (261 loc) · 6.51 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "chatroom_utils.h"
void trim_newline(char *text)
{
int len = strlen(text) - 1;
if (text[len] == '\n')
{
text[len] = '\0';
}
}
void clear_stdin_buffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
/* discard content*/;
}
// get a username from the user.
void get_username(char *username)
{
while (true)
{
printf("Enter a username: ");
fflush(stdout);
memset(username, 0, 1000);
fgets(username, 22, stdin);
trim_newline(username);
if (strlen(username) > 20)
{
// clear_stdin_buffer();
puts("Username must be 20 characters or less.");
}
else
{
break;
}
}
}
//send local username to the server.
void set_username(connection_info *connection)
{
message msg;
msg.type = SET_USERNAME;
strncpy(msg.username, connection->username, 20);
//
if (send(connection->socket, (void *)&msg, sizeof(msg), 0) < 0)
{
perror("Send failed");
exit(1);
}
}
void stop_client(connection_info *connection)
{
close(connection->socket);
exit(0);
}
//initialize connection to the server.
void connect_to_server(connection_info *connection, char *address, char *port)
{
while (true)
{
get_username(connection->username);
//Create socket
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));
//Connect to remote server
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.type = 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("/m <username> <message> Send a private message to given username.");
}
else if (strncmp(input, "/m", 2) == 0)
{
message msg;
msg.type = PRIVATE_MESSAGE;
char *toUsername, *chatMsg;
toUsername = strtok(input + 3, " ");
if (toUsername == NULL)
{
puts(KRED "The format for private messages is: /m <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;
}
//printf("|%s|%s|\n", toUsername, chatMsg);
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 //regular public message
{
message msg;
msg.type = PUBLIC_MESSAGE;
strncpy(msg.username, connection->username, 20);
// clear_stdin_buffer();
if (strlen(input) == 0)
{
return;
}
strncpy(msg.data, input, 255);
//Send some data
if (send(connection->socket, &msg, sizeof(message), 0) < 0)
{
perror("Send failed");
exit(1);
}
}
}
void handle_server_message(connection_info *connection)
{
message msg;
//Receive a reply from the server
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.type)
{
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("%s", msg.data);
break;
case SET_USERNAME:
//TODO: implement: name changes in the future?
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 TOO_FULL:
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;
//The fd_set data type represents file descriptor sets for the select function. It is actually a bit array.
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]);
//keep communicating with server
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;
}