-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.c
68 lines (48 loc) · 1.32 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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <ctype.h>
// Assume no input line will be longer than 1024 bytes
int main (int argc, char ** argv, char **envp) {
if(argc == 2){
fd_set chatfds;
char buffer[1024];
char connected[1024];
memset(connected, 0, 1024);
read(atoi(argv[1]), connected ,1024);
strcat(connected, " \r\n\r\n");
printf("to: %s\n", connected);
while(1){
FD_ZERO(&chatfds);
FD_SET(1, &chatfds);
FD_SET(atoi(argv[1]), &chatfds);
select(atoi(argv[1]) +1, &chatfds, 0, 0, 0);
if(FD_ISSET(1, &chatfds)){
memset(buffer, 0, 1024);
fgets(buffer, 1024, stdin);
strcpy(&buffer[strlen(buffer)-1], " \r\n\r\n");
write(atoi(argv[1]), buffer, strlen(buffer));
write(atoi(argv[1]), connected, strlen(connected));
//printf("You typed: %s\n", buffer);
} else if(FD_ISSET(atoi(argv[1]), &chatfds)){
memset(buffer, 0, 1024);
read(atoi(argv[1]), buffer ,1024);
if(!strcmp(buffer, "haha xd\n")){
printf("Press Any Key to exit\n");
getchar();
exit(0);
} else if(!strcmp(buffer, "/close")){
exit(0);
}
printf("%s", buffer);
}
}
} else {
printf("ERROR USAGE: ./chat <fd>\n");
}
}