-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundServer.c
More file actions
301 lines (280 loc) · 7.04 KB
/
Copy pathSoundServer.c
File metadata and controls
301 lines (280 loc) · 7.04 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<ctype.h>
#define BACKLOG 5
#define MAXLINE 4096
#define EINTR 3407
#define SERV_PORT 5035
char buff[MAXLINE];
int errorno;
int SIZE = 3;
int USER_SIZE = 1;
char animal[10][30] = {"DOG","CAT","LION"};
char voice[10][30] = {"WOOF","MEOW","ROAR"};
char username[10][30] = {"ADMIN"};
char password[10][30] = {"1234"};
char** query(int* k)
{
char** A;
int noe = 2*SIZE + 4,i;
A = (char**)malloc(noe * sizeof(char*));
int x = 0;
for(int j = 0;j<noe;j++)
A[j] = (char*)malloc(30 * sizeof(char));
strcpy(A[x++],"Server : ");
for(int i = 0;i < SIZE;i++)
{
strcpy(A[x++],animal[i]);
strcpy(A[x++],"\n");
}
strcat(A[x++],"QUERY:OK");
strcat(A[x++],"\n");
strcat(A[x++],"\nEnter message....\n");
*k = x;
return A;
}
char* search(char Animal[])
{
char *A = (char*)malloc(sizeof(char)*80);
for(int i = 0;i<SIZE;i++)
{
if(strcmp(animal[i],Animal) == 0)
{
strcpy(A,"Server : A ");strcat(A,animal[i]);strcat(A," SAYS ");
strcat(A,voice[i]);
strcat(A,"\n");
strcat(A,"\nEnter message....\n");
return A;
}
}
strcpy(A,"Server : I DON'T KNOW ");
strcat(A,Animal);
strcat(A,"\n");
strcat(A,"\nEnter message....\n");
return A;
}
void store(char Animal[] , char Voice[])
{
if(SIZE > 10)
return;
for(int i = 0;i<SIZE;i++)
{
if(strcmp(animal[i],Animal) == 0)
{
strcpy(voice[i] ,Voice);
return;
}
}
strcpy(animal[SIZE], Animal);
strcpy(voice[SIZE] ,Voice);
SIZE++;
}
int authenticate(char Username[], char Password[])
{
for(int i = 0;i<USER_SIZE;i++)
{
if(strcmp(username[i],Username) == 0)
{
if(strcmp(password[i],Password) == 0)
{
return 1;
}
else
return 0;
}
}
return 0;
}
void registerUser(char Username[], char Password[])
{
strcpy(username[USER_SIZE], Username);
strcpy(password[USER_SIZE], Password);
USER_SIZE++;
}
int main(int argc , char** argv)
{
int i , maxi ,maxfd ,listenfd, connfd,sockfd;
int nready ,n,client[FD_SETSIZE];
fd_set rset,allset;
socklen_t clilen; //length of client socket structure
struct sockaddr_in servaddr,cli_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Local IP address
servaddr.sin_port = htons(SERV_PORT); //Local Port Number
printf("********TCP Client-Server Application*********\n");
printf("******************TCP SERVER******************\n");
bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr));
printf("\nListening on port %d \n",SERV_PORT);
listen(listenfd, 10);
printf("\nWaiting for connections...\n");
maxfd = listenfd; //descriptor of listening socket
maxi = -1;
for(int i = 0;i<FD_SETSIZE;i++)
client[i] = -1;
FD_ZERO(&allset);
FD_SET(listenfd , &allset);
for(; ;)
{
rset = allset;
nready = select(maxfd+1 ,&rset,NULL,NULL,NULL);
//new client connection
if(FD_ISSET(listenfd , &rset))
{
clilen = sizeof(cli_addr);
if((connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen)) <0 ) //checking for client
{
if(errorno == EINTR)
continue;
else
{
perror("\nNot connected\n");
break;
}
}
for(i = 0;i<FD_SETSIZE;i++)
if(client[i] < 0)
{
client[i] = connfd;
break;
}
if(i == FD_SETSIZE)
{
perror("\nToo many clients...");
exit(0);
}
FD_SET(connfd,&allset);
if(connfd > maxfd)
maxfd = connfd;
if(i > maxi)
maxi = i;
if(--nready <= 0)
continue;
}
//check all clients for data
for(i = 0;i<= maxi;i++)
{
if((sockfd = client[i]) < 0)
continue;
if(FD_ISSET(sockfd , &rset))
{
int flag = 0;
if((n = read(sockfd , buff , MAXLINE)) == 0)
{
//connection closed by client
close(sockfd);
FD_CLR(sockfd , &allset);
client[i] = -1;
}
else
{
if(strncmp(buff,"REGISTER\n",n) == 0)
{
strcpy(buff,"Enter Username :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char U[n];
strncpy(U,buff,n-1);U[n-1] = '\0';
strcpy(buff,"Enter password :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char P[n];
strncpy(P,buff,n-1);P[n-1] = '\0';
registerUser(U,P);
//printf("1\n");
strcpy(buff,"Connected to server ...\n\nEnter message....\n");
flag = 1;
}
else if(strncmp(buff , "LOGIN\n",n) == 0)
{
strcpy(buff,"Enter Username :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char U[n];
strncpy(U,buff,n-1);U[n-1] = '\0';
strcpy(buff,"Enter password :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char P[n];
strncpy(P,buff,n-1);P[n-1] = '\0';
int c = authenticate(U,P);
if(c == 1)
strcpy(buff,"Connected to server ...\n");
else
strcpy(buff,"Server : Sorry your id and password is not correct:(\nEnter register/login to connect to server ...\n\nEnter message....\n");
}
else if(strncmp(buff , "SOUND\n",n)== 0)
{
printf("\nConnected to Client");
printf("%d",maxi);
printf("\n");
strcpy(buff ,"Server : SOUND:OK\n\nEnter message....\n");
}
else if(strncmp(buff,"QUERY\n",n)== 0)
{
int l = 0;
char** q = query(&l);
char one[MAXLINE];
//one[0] = "";
for(int i = 0;i<l;i++)
{
strcat(one,q[i]);
}
strcpy(buff,one);
one[0] = '\0';
}
else if(strncmp(buff , "STORE\n",n) == 0)
{
strcpy(buff,"Enter Animal :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char A[n];
strncpy(A,buff,n-1);A[n-1] = '\0';
strcpy(buff,"Enter Voice :\n");
write(sockfd,buff,MAXLINE);
n = read(sockfd , buff , MAXLINE);
char V[n-1];
strncpy(V,buff,n-1);V[n-1] = '\0';
store(A,V);
strcpy(buff,"Server : STORE:OK\n\nEnter message....\n");
}
else if(strncmp(buff,"BYE\n",n) == 0)
{
printf("\nClient");
printf("%d",i);
printf("is disconnected..\n");
strcpy(buff,"Server : BYE:OK\n");
write(sockfd,buff,MAXLINE);
close(sockfd);
FD_CLR(sockfd , &allset);
client[i] = -1;
}
else if(strncmp(buff , "END\n",n) == 0)
{
strcpy(buff,"Server : END:OK\n");
write(sockfd,buff,MAXLINE);
exit(0);
}
else
{
char extr[n];
strncpy(extr,buff,n-1);extr[n-1] = '\0';
strcpy(buff,search(extr));
}
write(sockfd,buff,MAXLINE);
if(--nready <= 0)
break;
}
}
}
}
return 0;
}