-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataserver.c
More file actions
356 lines (308 loc) · 8.06 KB
/
dataserver.c
File metadata and controls
356 lines (308 loc) · 8.06 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
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
352
353
354
355
356
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<fcntl.h>
#include <arpa/inet.h>
#define MAX 100000
int convertToInt(char*);
int main(int argc, char *argv[])
{
if(argc!=2){
printf("Oops Please give Server port no as Command Line Argument\n");
exit(1);
}
int portNo = convertToInt(argv[1]);
fd_set master;
fd_set read_fds;
struct sockaddr_in serveraddr,clientaddr;
char buf[MAX];
int maxfd1; // maximum file descriptor number + 1
int listenfd;
int newfd;
int bytes_read;
int addrlen = 5;
int i, j;
//clearing the master and read_fds set
FD_ZERO(&master);
FD_ZERO(&read_fds);
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Erro While Listening");
exit(1);
}
memset(&serveraddr, 0, sizeof(serveraddr));
memset(&clientaddr, 0, sizeof(clientaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(portNo);
//binding to port
if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1)
{
perror("Erro While binding");
exit(1);
}
printf("Server has successfully binded on port number %d\n",portNo);
//listening
if(listen(listenfd, 10) == -1)
{
perror("Error While trying to listen on given port");
exit(1);
}
// adding the listenfd to the master set
FD_SET(listenfd, &master);
maxfd1 = listenfd+1;
while(1)
{
// printf("reached here\n");
read_fds = master;
if(select(maxfd1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("Error returned by select");
exit(1);
}
for(i = 0; i <maxfd1; i++)
{
if(FD_ISSET(i, &read_fds))
{
if(i == listenfd)
{
//code to accept new connection
if((newfd = accept(listenfd, (struct sockaddr *)&clientaddr, &addrlen)) == -1)
{
perror("Error while Accepting connection");
}
else
{
if(newfd > maxfd1-1)
{
maxfd1 = newfd+1;
}
//adding new fd to master list
FD_SET(newfd, &master);
printf("Client with ip %s has successfully been connected on socket no %d\n ", inet_ntoa(clientaddr.sin_addr),newfd);
}
}
else
{
bzero(buf,MAX);
bytes_read = recv(i, buf, sizeof(buf), 0);
printf("command recieved %s from %d\n",buf,i);
//client has closed the connection
if(bytes_read == 0)
{
printf("Client on Socket No %d has closed the connection\n", i);
close(i);
//clearing the fd from masterset
FD_CLR(i, &master);
}
else if(bytes_read<0){
perror("error while receiving");
// exit(1);
continue;
}
else
{
if(strncmp(buf,"write",5)==0)
{
int ret=fork();
if(ret==0)
{
char name[100]={'\0'};
int k=0,j=0;
while(buf[k]!=' ')
{
k++;
}
k++;
name[0]='n';
name[1]='e';
name[2]='w';
j=3;
while(buf[k])
{
name[j++]=buf[k++];
}
printf("buffer [%s] file name %s \n",buf,name);
send(i,"done",sizeof("done"),0);
FILE *fp=fopen(name,"wb");
if(fp<0)
{
printf("eroornopening file\n");
bzero(buf,MAX);
strcat(buf,"file error\n");
if(send(i, buf, MAX, 0) == -1)
perror("Error Sending Data");
continue;
}
bzero(buf,MAX);
int n=recv(i,buf,sizeof(buf),0);
int nooftime=atoi(buf);
printf("no of times:[%d]\n",nooftime);
// printf("sending");
// int no=0;
int loop=nooftime;
while(nooftime>=10000)
{
unsigned char a[10000]={'\0'};
send(i,"done",4,0);
// if(nooftime%10000==0)
// printf(".");
fflush(stdout);
n=recv(i,a,sizeof(a),0);
// printf("result recieved[%s]\n",buf);
// printf("n is %ld\n",strlen(buf));
// fputc(a,fp);
fwrite(a,10000,1,fp);
// no=n+no;
nooftime=nooftime-10000;
}
if(loop%10000!=0)
{
unsigned char a[10000]={'\0'};
send(i,"done",4,0);
// if(nooftime%10000==0)
// printf(".");
fflush(stdout);
n=recv(i,a,loop%10000,0);
// printf("result recieved[%s]\n",buf);
// printf("n is %ld\n",strlen(buf));
// fputc(a,fp);
fwrite(a,loop%10000,1,fp);
// no=n+no;
// nooftime=nooftime-10000;
}
fclose(fp);
// printf("n %d",no);
// printf("\n");
bzero(buf,MAX);
// bzero(result,MAX);
strcat(buf,"succesffull\n");
if(send(i, buf, MAX, 0) == -1)
perror("Error Sending Data");
}
else
{
// printf("Client on Socket No %d has closed the connection\n", i);
close(i);
//clearing the fd from masterset
FD_CLR(i, &master);
maxfd1=maxfd1-1;
}
}
else
{
if(strncmp(buf,"read",4)==0)
{
int ret=fork();
if(ret==0)
{
char name[100]={'\0'};
int k=0,j=0;
while(buf[k]!=' ')
{
k++;
}
k++;
name[0]='n';
name[1]='e';
name[2]='w';
j=3;
while(buf[k])
{
name[j++]=buf[k++];
}
printf("buffer [%s] file name %s \n",buf,name);
FILE *fp=fopen(name,"rb");
if(fp<0)
{
printf("eroornopening file\n");
bzero(buf,MAX);
strcat(buf,"file error\n");
if(send(i, buf, MAX, 0) == -1)
perror("Error Sending Data");
continue;
}
fseek(fp,0,SEEK_END);
int len = ftell(fp);
fseek(fp,0,SEEK_SET);
// int nooftimes=len/1000;
printf("no of times%d\n",len);
char x[1000];
sprintf(x,"%d",len);
send(i,x,sizeof(x),0);
bzero(buf,MAX);
int n=recv(i,buf,sizeof(buf),0);
// printf("sending");
int no=0;
int loop=len;
while(len>=10000)
{
unsigned char a[10000]={'\0'};
j=0;
// a=fgetc(fp);
fread(a,10000,1,fp);
send(i,a,sizeof(a),0);
bzero(buf,MAX);
recv(i,buf,4,0);
bzero(buf,sizeof(buf));
len=len-10000;
}
if(loop%10000!=0)
{
unsigned char a[10000]={'\0'};
j=0;
// a=fgetc(fp);
fread(a,loop%10000,1,fp);
send(i,a,loop%10000,0);
bzero(buf,MAX);
recv(i,buf,4,0);
bzero(buf,sizeof(buf));
}
fclose(fp);
// char cmd[1010]={'\0'};
// sprintf(cmd,"rm %s",name);
// system(cmd);
printf("n %d",no);
printf("\n");
bzero(buf,MAX);
// bzero(result,MAX);
strcat(buf,"succesffull\n");
if(send(i, buf, MAX, 0) == -1)
perror("221Error Sending Data");
}
else
{
// printf("Client on Socket No %d has closed the connection\n", i);
close(i);
//clearing the fd from masterset
FD_CLR(i, &master);
maxfd1=maxfd1-1;
}
}
bzero(buf,MAX);
strcat(buf,"no command\n");
if(send(i, buf, MAX, 0) == -1)
perror("2121Error Sending Data");
}
}
}
}
}
}
return 0;
}
// Start of utility function to convert character to integer
int convertToInt(char *in){
int i = strlen(in)-1,multiplier=1,result = 0;
while(i>=0){
result+=(in[i]-48)*multiplier;
multiplier*=10;
i--;
}
return result;
}
//End of utility function to convert character to integer