-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
351 lines (291 loc) · 6.77 KB
/
server.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
/*
* basic chat application for linux env
* used TCP connection
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
typedef struct detail_client{
struct detail_client *next;
char name[10];
int fd;
}detail;
detail *head, *temp1;
int fd = -1;
void *connection_handler(void *);
int global_decs_table[10], i = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("bind failed. Error");
return 1;
}
puts("bind done");
listen(socket_desc , 3);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
global_decs_table[i++] = client_sock; //feature ref, now i used list for store session info.
/* Handler thread for handle clien's */
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
* connection_handler
*
* Handler function handle all client connection.
*/
void *connection_handler(void *socket_desc)
{
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
int sock1 = global_decs_table[1];
char buffer[10], buffer1[2000];;
message = "Greetings! First u must register your name on server usrname size must 6 char.\nsend one usr to another usr u must know ur friend id or name";
if (write(sock , message , strlen(message)) == -1)
perror("write faild");
memset(client_message, 0, sizeof(client_message));
if(!(read_size = read(sock , client_message , 2000)))
perror("read fail not store your deatails in server");
#if DEBUG
printf("client message %s fd %d\n", client_message, sock);
#endif
client_details(client_message, buffer);
add_data_to_list(buffer, sock);
memset(client_message, 0, sizeof(client_message));
memset(buffer1, 0, sizeof(client_message));
while( (read_size = read(sock , client_message , 2000)) > 0 ){
pthread_mutex_lock(&lock);
client_details(client_message, buffer);
printf("send to %s\n", buffer);
cmb_fd(buffer1, sock);
cmb_name(buffer, buffer1, client_message, sock);
#if 0
if(fd == -1)
printf("connection failure\n");
else
printf("alive online\n");
#endif
#if DEBUG
printf("%s final message: fd %d\n", buffer1, fd);
#endif
if (write(fd , buffer1 , strlen(buffer1)) == -1)
perror("server");
memset(client_message, 0, sizeof(client_message));
memset(buffer1, 0, sizeof(buffer1));
pthread_mutex_unlock(&lock);
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
free(socket_desc);
return 0;
}
/*
* add_data_to_list
* add client info to list (client username and socket)
*/
int add_data_to_list(char *buffr, int fd)
{
do{
if(head == NULL){
detail *temp;
temp = (detail *)malloc(sizeof (*temp));
if((temp = (detail *)malloc(sizeof (*temp))) == NULL){
printf("malloc fail\n");
break;
}
memset(temp, 0, sizeof(*temp));
head = temp;
strcpy(head -> name, buffr);
head -> fd = fd;
temp1 = head;
}else{
detail *temp;
if((temp = (detail *)malloc(sizeof (*temp))) == NULL){
printf("malloc fail\n");
break;
}
strcpy(temp -> name, buffr);
printf("add list %s\n", temp -> name);
temp -> fd = fd;
temp1 -> next = temp;
temp1 = temp1 -> next;
}
}while(0);
return 0;
}
/*
* client_details
*
* Get the first 6 digit (client name should 6 digit(current design)).
*/
int client_details(char *clnt_message, char *buffer)
{
char *temp = clnt_message, i = 0;
while(i < 6){
*(buffer + i++) = *temp++;
}
*(buffer + i) = '\0';
strcpy(clnt_message, temp);
return 0;
}
int display()
{
printf("name %s fd %d addr %g\n", head -> name, head -> fd, head -> next);
return 0;
}
/*
* cmb_name
* Get destination file descriptor from list
*/
int cmb_name(char *buffer, char *buffer1, char *client_message, int sock)
{
detail *temp;
temp = head;
int t = 1;
#if 1
do{
if(temp == NULL){
printf("head also null no client connect\n");
t = 2;
break;
}
if(temp -> next == NULL){
fd = temp -> fd;
if(strcmp(temp -> name, buffer) == 0){
fd = temp -> fd;
ap_nd(buffer1, client_message);
break;
}
memset(buffer1, 0, strlen(buffer1));
strcpy(buffer1, "Network");
ap_nd(buffer1, "no one online");
t = 3;
break;
}
while(temp -> next != NULL){
if(strcmp(temp -> name, buffer) == 0){
fd = temp -> fd;
memset(buffer1, 0, strlen(buffer));
ap_nd(buffer1, client_message);
goto end;
}
t++;
temp = temp -> next;
}
if(strcmp(temp -> name, buffer) == 0){
fd = temp -> fd;
ap_nd(buffer1, client_message);
} else{
fd = sock;
memset(buffer1, 0, strlen(buffer));
strcpy(buffer1, "Network");
ap_nd(buffer1, "not connect");
}
end:
break;
}while(0);
#endif
return 0;
}
/*
* cmb_fd
* Get username via fd
*/
int cmb_fd(char *buffer, int fd)
{
detail *temp;
temp = head;
int t = 1;
#if 1
do{
if(temp == NULL){
break;
}
if(temp -> next == NULL){
if(fd == temp -> fd){
strcpy(buffer, temp -> name);
fd = temp -> fd;
break;
}
fd = temp -> fd;
printf("any one in online..i send back message to u\n");
break;
}
while(temp -> next != NULL){
if(fd == temp -> fd){
strcpy(buffer, temp -> name);
break;
}
t++;
temp = temp -> next;
}
if(fd == temp -> fd)
strcpy(buffer, temp -> name);
else
strcpy(buffer, "Netw0rk");
}while(0);
#endif
return 0;
}
int ap_nd(char *dest, char *sour)
{
char *temp = dest;
while(*temp != '\0'){
temp++;
}
*temp++ = ':';
while(*sour != '\0'){
*temp++ = *sour++;
}
*temp++ = '\0';
return 0;
}
int delay(int loop)
{
int i = 0;
for(i = 0; i < loop; i++);
}