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
|
/**
* Localchat
* Ben Burwell
*
* Chat request accepting functionality
*/
void *receive_chat_messages(void *arg) {
char in_buf[1024];
int length = 1024;
int retcode;
retcode = recv(client_s, in_buf, length, 0);
printf("%s \n", in_buf);
while (strcmp(in_buf, "SESQ")) {
retcode = recv(client_s, in_buf, length, 0);
if (retcode > 0) {
printf("%s \n", in_buf);
}
}
}
void *chat_acceptor(void *arg) {
int client_s;
char in_buf[4096];
char out_buf[4096];
int length = 4096;
char command[4096];
int retcode;
int i;
char * token;
struct sockaddr_in client_addr;
client_s = (int) arg;
retcode = recv(client_s, in_buf, length, 0);
if (strcmp(in_chat, "Y") == 0) {
// if already in a chat, reply no and close
strcpy(out_buf, "SESANS:N");
send(client_s, out_buf, strlen(out_buf)+1, 0);
close(client_s);
} else {
// not in a chat, ask the user
char ans[4];
printf("\n*** CHAT REQUEST *** \n");
printf("Accept [y/n]? ");
fflush(stdout);
fgets(ans, 2, stdin);
if (strcmp(ans, "y") == 0) {
strcpy(out_buf, "SESANS:Y");
send(client_s, out_buf, strlen(out_buf)+1, 0);
char inp[256];
pthread_t recv_thread;
pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
while (strcmp(inp, "/q\n") != 0) {
printf("chat> ");
fgets(inp, 256, stdin);
if (strcmp(inp, "/q\n") != 0) {
strcpy(out_buf, "SESMSG:");
strcat(out_buf, inp);
send(client_s, out_buf, strlen(out_buf)+1, 0);
} else {
strcpy(out_buf, "SESQ");
send(client_s, out_buf, strlen(out_buf)+1, 0);
}
}
} else {
strcpy(out_buf, "SESANS:N");
send(client_s, out_buf, strlen(out_buf)+1, 0);
}
}
close(client_s);
return;
}
void *server_loop(void *arg) {
int server_s;
struct sockaddr_in server_addr;
int client_s;
struct sockaddr_in client_addr;
struct in_addr client_ip_addr;
socklen_t addr_len;
pthread_t thread_id;
int retcode;
server_s = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(CHAT_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
retcode = bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (retcode < 0) {
printf("Network error, quitting... \n");
exit(-1);
}
listen(server_s, 100);
while (1) {
addr_len = sizeof(client_addr);
client_s = accept(server_s, (struct sockaddr *)&client_addr, &addr_len);
if (client_s == -1) {
printf("Network error, quitting... \n");
exit(-1);
}
pthread_create(&thread_id, NULL, chat_acceptor, (void *)client_s);
}
return;
}
void start_accepting_chat_requests() {
pthread_t thread;
pthread_create(&thread, NULL, server_loop, NULL);
}
|