-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.c
320 lines (276 loc) · 7.14 KB
/
server2.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
/*
* Copyright 2014
*
* Author: Dinux
* Description: Simple chatroom in C
* Version: 1.0
*
*/
#include <sys/socket.h>//
#include <netinet/in.h>//
#include <arpa/inet.h>
#include <stdio.h>//
#include <stdlib.h>//
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>//
#include <sys/types.h>//
#include <signal.h>//
#define MAX_CLIENTS 100
static unsigned int cli_count = 0;
static int uid = 10;
/* Client structure */
typedef struct
{
struct sockaddr_in addr; /* Client remote address */
int connfd; /* Connection file descriptor */
int uid; /* Client unique identifier */
char name[50]; /* Client name */
} client_t;
client_t *clients[MAX_CLIENTS]; //
/* Add client to queue */
void queue_add(client_t *cl){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(!clients[i])
{
clients[i] = cl;
return;
}
}
}
/* Delete client from queue */
void queue_delete(int uid){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(clients[i]){
if(clients[i]->uid == uid){
clients[i] = NULL;
return;
}
}
}
}
/* Send message to all clients but the sender */
void send_message(char *s, int uid){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(clients[i]){
if(clients[i]->uid != uid){
write(clients[i]->connfd, s, strlen(s));
}
}
}
}
/* Send message to all clients */
void send_message_all(char *s){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(clients[i]){
write(clients[i]->connfd, s, strlen(s));
}
}
}
/* Send message to sender */
void send_message_self(const char *s, int connfd){
write(connfd, s, strlen(s));
}
/* Send message to client */
void send_message_client(char *s, int uid){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(clients[i]){
if(clients[i]->uid == uid){
write(clients[i]->connfd, s, strlen(s));
}
}
}
}
/* Send list of active clients */
void send_active_clients(int connfd){
int i;
char s[64];
for(i=0;i<MAX_CLIENTS;i++){
if(clients[i]){
sprintf(s, "<<CLIENT %d | %s\r\n", clients[i]->uid, clients[i]->name);
send_message_self(s, connfd);
}
}
}
/* Strip CRLF */
void strip_newline(char *s){
while(*s != '\0'){
if(*s == '\r' || *s == '\n'){
*s = '\0';
}
s++;
}
}
/* Print ip address */
void print_client_addr(struct sockaddr_in addr){
printf("%d.%d.%d.%d",
addr.sin_addr.s_addr & 0xFF,
(addr.sin_addr.s_addr & 0xFF00)>>8,
(addr.sin_addr.s_addr & 0xFF0000)>>16,
(addr.sin_addr.s_addr & 0xFF000000)>>24);
}
/* Handle all communication with the client */
void *handle_client(void *arg)
{
char buff_out[1024];
char buff_in[1024];
int rlen;
cli_count++;
client_t *cli = (client_t *)arg;
printf("<<ACCEPT ");
print_client_addr(cli->addr);
printf(" REFERENCED BY %d\n", cli->uid);
sprintf(buff_out, "<<JOIN, HELLO %s\r\n", cli->name);
send_message_all(buff_out);
printf("before while\n");
/* Receive input from client */
printf(rlen);
int v = read(cli->connfd, buff_in, sizeof(buff_in)-1);
printf("v ");
printf("%d",v);
while((rlen = read(cli->connfd, buff_in, sizeof(buff_in)-1)) > 0)
{
printf("hiiiii\n");
buff_in[rlen] = '\0';
buff_out[0] = '\0';
//strip_newline(buff_in);
/* Ignore empty buffer */
if(!strlen(buff_in)){
continue;
}
/* Special options */
// if(buff_in[0] == '\\'){
// char *command, *param;
// command = strtok(buff_in," ");
// if(!strcmp(command, "\\QUIT")){
// break;
// }else if(!strcmp(command, "\\PING")){
// send_message_self("<<PONG\r\n", cli->connfd);
// }else if(!strcmp(command, "\\NAME")){
// param = strtok(NULL, " ");
// if(param){
// char *old_name = strdup(cli->name);
// strcpy(cli->name, param);
// sprintf(buff_out, "<<RENAME, %s TO %s\r\n", old_name, cli->name);
// free(old_name);
// send_message_all(buff_out);
// }else{
// send_message_self("<<NAME CANNOT BE NULL\r\n", cli->connfd);
// }
// }else if(!strcmp(command, "\\PRIVATE")){
// param = strtok(NULL, " ");
// if(param){
// int uid = atoi(param);
// param = strtok(NULL, " ");
// printf("%s\n",param );
// if(param){
// sprintf(buff_out, "[PM][%s]", cli->name);
// while(param != NULL){
// strcat(buff_out, " ");
// strcat(buff_out, param);
// param = strtok(NULL, " ");
// }
// strcat(buff_out, "\r\n");
// send_message_client(buff_out, uid);
// }else{
// send_message_self("<<MESSAGE CANNOT BE NULL\r\n", cli->connfd);
// }
// }else{
// send_message_self("<<REFERENCE CANNOT BE NULL\r\n", cli->connfd);
// }
// }else if(!strcmp(command, "\\ACTIVE")){
// sprintf(buff_out, "<<CLIENTS %d\r\n", cli_count);
// send_message_self(buff_out, cli->connfd);
// send_active_clients(cli->connfd);
// }else if(!strcmp(command, "\\HELP")){
// strcat(buff_out, "\\QUIT Quit chatroom\r\n");
// strcat(buff_out, "\\PING Server test\r\n");
// strcat(buff_out, "\\NAME <name> Change nickname\r\n");
// strcat(buff_out, "\\PRIVATE <reference> <message> Send private message\r\n");
// strcat(buff_out, "\\ACTIVE Show active clients\r\n");
// strcat(buff_out, "\\HELP Show help\r\n");
// send_message_self(buff_out, cli->connfd);
// }else{
// send_message_self("<<UNKOWN COMMAND\r\n", cli->connfd);
// }
// }
if(buff_in[0] != '\\')
{
/* Send message */
sprintf(buff_out, "[%s] %s\r\n", cli->name, buff_in);
send_message(buff_out, cli->uid);
}
}
printf("after while\n");
/* Close connection */
close(cli->connfd);
printf("after close\n");
sprintf(buff_out, "<<LEAVE, BYE %s\r\n", cli->name);
send_message_all(buff_out);
/* Delete client from queue and yeild thread */
queue_delete(cli->uid);
printf("<<LEAVE ");
print_client_addr(cli->addr);
printf(" REFERENCED BY %d\n", cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
/* Socket settings */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
/* Bind */
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
perror("Socket binding failed");
return 1;
}
/* Listen */
if(listen(listenfd, 10) < 0){
perror("Socket listening failed");
return 1;
}
printf("<[SERVER STARTED]>\n");
/* Accept clients */
while(1)
{
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
/* Check if max clients is reached */
if((cli_count+1) == MAX_CLIENTS){
printf("<<MAX CLIENTS REACHED\n");
printf("<<REJECT ");
print_client_addr(cli_addr);
printf("\n");
close(connfd);
continue;
}
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->addr = cli_addr;
cli->connfd = connfd;
cli->uid = uid++;
sprintf(cli->name, "%d", cli->uid);
/* Add client to the queue and fork thread */
queue_add(cli);
pthread_create(&tid, NULL, &handle_client, (void*)cli);
/* Reduce CPU usage */
sleep(1);
}
}