This repository has been archived by the owner on Nov 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
434 lines (400 loc) · 11.9 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#define MSG_SIZE 200
#define NAME_SIZE 20
#define MAX_SIZE 1000
#define MYPORT 7400
#define LENGTH 1024
#define CFG_ID_KEY 1
#define CFG_NICK_KEY 2
void error(const char *msg)
{
perror(msg);
exit(1);
}
char *trimWhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
void analyzeCommand(char* message, char* command, char* receiver, char* content){
/* init */
strcpy(receiver, "");
strcpy(content, "");
strcpy(command, "");
if (message == NULL || strlen(message) == 0)
return;
if (message[0] == '/'){
/* start process command */
sscanf(message+1, "%s %s %[^\n]",command, receiver, content);
return;
}
strcpy(content, message);
}
/*
Message format:
M[receiver][message] -> send simple message
F[receiver][filedescription] -> communicate before transfer
T[id][package] -> upload file follow id
D[id][filedescription] -> communicate before download
C[key]:[value] -> send configure (ex: nickname register)
N[notify string] -> notify user
L[users/files] -> list all users/ files (that user can download)
X[message] -> client exit
receiver's length: NAME_SIZE(20)
key's length: 2
*/
int main(int argc, char *argv[]) {
int i=0;
int port;
int server_sockfd, client_sockfd;
struct sockaddr_in server_address;
int addresslen = sizeof(struct sockaddr_in);
int fd;
fd_set readfds, testfds, clientfds;
char msg[MSG_SIZE + 1];
char kb_msg[MSG_SIZE + 22];
char revbuf[LENGTH];
char sdbuf[LENGTH];
char pkgbuf[LENGTH+5];
int fs_block_sz;
char fileTransfer[MSG_SIZE];
char fileDownload[MSG_SIZE];
FILE *f, *fr;
int isUploading;
/*Client variables=======================*/
int sockfd;
int result;
char nickname[NAME_SIZE];
char hostname[MSG_SIZE];
int is_nickname_set;
struct hostent *hostinfo;
struct sockaddr_in address;
int clientid;
/*Client==================================================*/
if(argc==2 || argc==4){
if(!strcmp("-p",argv[1])){
if(argc==2){
printf("Invalid parameters.\nUsage: chat [-p PORT] HOSTNAME\n");
exit(0);
}else{
sscanf(argv[1],"%i",&port);
strcpy(hostname,argv[3]);
}
}else{
port=MYPORT;
strcpy(hostname,argv[1]);
}
printf("\n*** Client program starting (enter \"quit\" to stop):\n");
fflush(stdout);
/* Create a socket for the client */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
/* Name the socket, as agreed with the server */
hostinfo = gethostbyname(hostname); /* look for host's name */
address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;
address.sin_family = AF_INET;
address.sin_port = htons(port);
/* Connect the socket to the server's socket */
if(connect(sockfd, (struct sockaddr *)&address, sizeof(address)) < 0){
perror("connecting");
exit(1);
}
printf("Connected to server, please set a nickname to start chat\n");
/* init client variable */
memset(nickname, 0, MSG_SIZE); /* init nickname to zero */
strcpy(fileTransfer, "");
strcpy(fileDownload, "");
clientid = -1;
is_nickname_set = 0;
isUploading = 0;
fflush(stdout);
FD_ZERO(&clientfds);// init client description files
FD_SET(sockfd,&clientfds);// add socket client description file
/* Now wait for messages from the server */
while (1) {
testfds=clientfds;
select(FD_SETSIZE,&testfds,NULL,NULL,NULL);
for(fd=0;fd<FD_SETSIZE;fd++){
if(FD_ISSET(fd,&testfds)){
if(fd==sockfd){ /*Accept data from open socket */
//read data from open socket
result = read(sockfd, pkgbuf, LENGTH+5);
if (clientid < 0 || !is_nickname_set){ /* set client id before start chat */
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
if(msg[0] == 'C'){
int key = -1;
char temp[MSG_SIZE];
sscanf(msg, "C%2d:%s", &key, temp);
switch (key){
case CFG_ID_KEY: /* configure clientid */
clientid = atoi(temp);
if (clientid < 0) break;
printf("You are signed in with id: %d\n", clientid);
/* require nickname before start chat */
do {
printf("Your nickname(no space):");
scanf("%s",nickname);
fflush(stdout);
if (nickname[0] == 0) printf("Please set nickname before start\n");
else {
/* send nickname to server */
sprintf(msg, "C%2d:%s", CFG_NICK_KEY, nickname);
write(sockfd, msg, strlen(msg));
break;
}
} while(1);
break;
case CFG_NICK_KEY:
printf("Your nickname %s is set. You can start chat now.\n", temp);
is_nickname_set = 1;
printf("%s>",nickname);
fflush(stdout);
strcpy(nickname, temp);
FD_SET(0,&clientfds); /* add keyboard listen from here */
break;
}
} else if (msg[0] == 'N'){
printf("%s\n",msg+1);
/* require nickname before start chat */
do {
printf("Your nickname(no space):");
scanf("%s",nickname);
if (nickname[0] == 0) printf("Please set nickname before start\n");
else {
/* send nickname to server */
sprintf(msg, "C%2d:%s", CFG_NICK_KEY, nickname);
write(sockfd, msg, strlen(msg));
break;
}
} while(1);
}
} else { /* start show chat when clientid is set */
char code = pkgbuf[0];
char temp[MSG_SIZE], name[MSG_SIZE];
char* trimName;
int id;
switch(code){
case 'M':
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
strcpy(temp, msg+1);
temp[NAME_SIZE] = '\0';
char* trimName = trimWhitespace(temp);
char sender[NAME_SIZE+1];
if (strcmp(trimName,"") == 0){
strcpy(sender, "Server");
} else {
strcpy(sender, trimName);
}
printf("\n-->(%s) %s\n",sender, msg+21);
printf("%s>",nickname);
fflush(stdout);
break;
case 'N':
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
printf("\nWARN: %s\n",msg+1);
printf("%s>",nickname);
fflush(stdout);
break;
case 'X':
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
printf("%s\n",msg+1);
close(sockfd); //close the current client
exit(0);
break;
case 'F': /* receive start signal to transfer file */
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
if (!isUploading){ /* check if uploading or not*/
/* start transfer id */
strncpy(temp, msg+1, 4);
temp[4] = '\0';
printf("\nstart tranfer file to server...%s|\n",msg);
/* open file if need */
int error = 0;
if (strcmp(fileTransfer, "")!= 0){
if ((f = fopen(fileTransfer, "r")) == NULL){
printf("Error: Cant open file %s\n", fileTransfer);
error = 1;
}
}
if (!error){
memset(sdbuf, 0, LENGTH);
sprintf(pkgbuf, "T%4s", temp);
/* start transfer */
isUploading = 1;
while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, f)) > 0){
memcpy(pkgbuf+5, sdbuf, fs_block_sz);
write(sockfd, pkgbuf, fs_block_sz+5);
memset(sdbuf, 0, LENGTH);
}
printf("transfer successfully\n");
isUploading = 0;
strcpy(fileTransfer, "");
fflush(stdout);
fclose(f);
}
}else
printf("\nfile %s is in uploading\n", fileTransfer);
printf("%s>",nickname);
fflush(stdout);
break;
case 'D': /* communicate before download */
memcpy(msg, pkgbuf, result);
msg[result] = '\0'; /* Terminate string with null */
memset(temp, 0, strlen(temp));
strcpy(temp, msg+21);
temp[20]='\0';
strcpy(name, msg+1);
name[20]='\0';
char* trim = trimWhitespace(name);
strcpy(fileDownload, temp);
if ((fr=fopen(fileDownload, "w")) == NULL){
printf("\nError: Cant open file %s\n", fileDownload);
printf("Download fail\n");
strcpy(fileDownload, ""); /* rollback */;
}else{
fclose(fr); /* just create blank file */
strcpy(temp, msg+41);
printf("\nYou can download file %s from %s use id:%s\n",fileDownload, trim, temp);
}
printf("%s>",nickname);
fflush(stdout);
break;
case 'T': /* dowload file */
if (strcmp(fileDownload, "") != 0){
if ((fr=fopen(fileDownload, "a")) == NULL){
printf("\nError: Cant open file %s\n", fileDownload);
printf("Download fail\n");
strcpy(fileDownload, ""); /* rollback */;
} else {
int count =fwrite(pkgbuf+5, sizeof(char), result-5, fr);
fclose(fr);
if (result < LENGTH + 5) { /* download finish */
printf("\nDownload finished\n");
strcpy(fileDownload, "");
}
}
if (strcmp(fileDownload, "") == 0){
printf("%s>", nickname);
fflush(stdout);
}
}
break;
case 'L':
memcpy(temp, pkgbuf+1, 5);
temp[5] = '\0';
printf("\n");
if (strcmp(temp,"users")==0){
for (i=0;i < result-6;i+=20){
memcpy(msg, pkgbuf+6+i, 20);
msg[20] = '\0';
char* trim = trimWhitespace(msg);
strcpy(temp, trim);
printf("user: %s|\n",temp);
}
} else if(strcmp(temp, "files")==0){
for (i=0;i<result-6;i+=25){
memcpy(msg, pkgbuf+6+i, 25);
msg[25] = '\0';
sscanf(msg,"%4d:%20s",&id, temp);
char* trim = trimWhitespace(temp);
strcpy(temp, trim);
printf("file: id=%d; name=%s|\n",id, temp);
}
}
printf("%s>",nickname);
fflush(stdout);
break;
default:
break;
}
memset(msg, 0, MSG_SIZE+1);
memset(kb_msg, 0, MSG_SIZE+22);
memset(pkgbuf, 0, LENGTH+5);
}
}
else if(fd == 0){ /*process keyboard activiy*/
fgets(kb_msg, MSG_SIZE+1, stdin);
// remove \n at end before process
kb_msg[strlen(kb_msg)-1] = '\0';
if (strcmp(kb_msg, "quit")==0) {
sprintf(msg, "XClient[%s] is shutting down.\n",nickname);
write(sockfd, msg, strlen(msg));
close(sockfd); //close the current client
exit(0); //end program
}
else {
if (strlen(kb_msg) > 0){
char command[MSG_SIZE];
char content[MSG_SIZE];
char receiver[NAME_SIZE];
analyzeCommand(kb_msg, command, receiver, content);
//printf("analyze %s-%s-%s\n",command, receiver, content);
if (strcmp(command,"") == 0 || strcmp(command,"to") == 0){
/* send message */
sprintf(msg, "M%20s%s", receiver,content);
write(sockfd, msg, strlen(msg));
} else if (strcmp(command,"file")==0
&& strcmp("",receiver)!=0
&& strcmp("",content)!=0){
/* communicate before transfer
content in this case is file name*/
strcpy(fileTransfer, content);
sprintf(kb_msg, "F%20s%s", receiver, fileTransfer);
write(sockfd, kb_msg, strlen(kb_msg));
} else if (strcmp(command, "down") == 0
&& strcmp("", receiver) != 0){
/* download file use id
receiver in this case is id*/
int id = atoi(receiver);
if (id >= 0){
sprintf(msg, "D%4d", id);
write(sockfd, msg, strlen(msg));
}
} else if (strcmp(command, "list") == 0
&& strcmp("users", receiver) == 0){
/* list all users in server*/
strcpy(msg, "Lusers");
write(sockfd, msg, strlen(msg));
} else if (strcmp(command, "list") == 0
&& strcmp("files", receiver) == 0){
/* list all files in server*/
strcpy(msg, "Lfiles");
write(sockfd, msg, strlen(msg));
}
printf("%s>",nickname);
fflush(stdout);
}
memset(msg, 0, MSG_SIZE+1);
memset(kb_msg, 0, MSG_SIZE+22);
}
}
}
}
}
}// end client code
}//main