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
|
/**
* Localchat
* Ben Burwell
*
* Chat request accepting functionality
*/
void *receive_chat_messages(void *arg) {
char in_buf[GLOBAL_MSG_LENGTH];
int retcode;
int i;
char separators[4] = ":";
char * tok;
retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
while (strcmp(in_buf, "SESQ") != 0) {
if (retcode > 0) {
char *message;
message = strtok(in_buf, separators);
if (strcmp(message, "SESMSG") == 0) {
printf("\n----> %s", strtok(NULL, separators));
printf("%s ", prompt);
fflush(stdout);
}
}
strcpy(in_buf, "");
retcode = recv(client_s, in_buf, sizeof(in_buf), 0);
}
printf("\n*** Peer closed the session. *** \n");
// close the socket
close(client_s);
// update chat flag and broadcast
strcpy(in_chat, "N");
status_broadcast_once();
// reset the prompt
strcpy(prompt, ">");
printf("%s ", prompt);
fflush(stdout);
return;
}
void *chat_acceptor(void *arg) {
accepted_client = (int) arg;
int retcode;
char buf_i[GLOBAL_MSG_LENGTH];
char buf_o[GLOBAL_MSG_LENGTH];
retcode = recv(accepted_client, buf_i, sizeof(buf_i), 0);
if (strcmp(in_chat, "Y") == 0) {
// if already in a chat, reply no and close
strcpy(buf_o, "SESANS:N");
send(accepted_client, buf_o, strlen(buf_o)+1, 0);
close(accepted_client);
} else {
// not in a chat, ask the user
printf("\n*** CHAT REQUEST *** \n");
printf("Accept [y/n]? ");
fflush(stdout);
// update the request time
time(&chat_requested_time);
respond_to_chat_request = 1;
}
return;
}
void accept_callback_accept() {
client_s = accepted_client;
strcpy(out_buf, "SESANS:Y");
send(client_s, out_buf, strlen(out_buf)+1, 0);
char inp[256];
// update the prompt
strcpy(prompt, "chat>");
// update chat flag and broadcast
strcpy(in_chat, "Y");
status_broadcast_once();
// print info message
printf("Entering chat mode. Type /q to exit. \n");
pthread_t recv_thread;
pthread_create(&recv_thread, NULL, receive_chat_messages, NULL);
return;
}
void accept_callback_decline() {
char buf[GLOBAL_MSG_LENGTH];
strcpy(buf, "SESANS:N");
send(accepted_client, buf, strlen(buf)+1, 0);
close(accepted_client);
return;
}
void *server_loop(void *arg) {
int server_s;
struct sockaddr_in server_addr;
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(accepted_addr);
accepted_client = accept(server_s, (struct sockaddr *)&accepted_addr, &addr_len);
if (accepted_client == -1) {
printf("Network error, quitting... \n");
exit(-1);
}
pthread_create(&thread_id, NULL, chat_acceptor, (void *)accepted_client);
}
return;
}
void start_accepting_chat_requests() {
pthread_t thread;
pthread_create(&thread, NULL, server_loop, NULL);
}
|