-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
executable file
·73 lines (56 loc) · 1.63 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
#include <stdio.h>
#include "network.h"
#include "error.h"
// Author: Gordon Gao
// Decides whether to corrupt frames coming from the server.
int ServerCorrupt(int dataFrames, int ackFrames, FrameType frameType) {
return ackFrames % 13 == 0 && frameType == FT_ACK;
}
int main(int argc, char *argv[]) {
// phy_host readies server to receive connections
phy_host();
//dat_setShouldCorrupt(ServerCorrupt);
while(1)
{
// Gets new socket
int clientsock = phy_accept();
// Forks and sets to new Socket
if( fork() == 0)
{
phy_setSocket(clientsock);
break;
}
}
int id,numphotos,i;
net_handshake(&id, &numphotos);
char photoFile[100];
char filename[20];
sprintf(filename, "server_%d.log", id);
logfile = fopen(filename, "w");
for(i = 0;i < numphotos; i++)
{
strcpy ( photoFile, "photonew");
// Appends input into one string for file access
// photoij.jpg
char idIndex[5];
sprintf(idIndex,"%d", id);
strcat(photoFile, idIndex);
char photoIndex[5];
sprintf(photoIndex, "%d", i);
strcat(photoFile, photoIndex);
strcat(photoFile, ".jpg");
FILE *file = fopen(photoFile, "w");
int endOfPhoto;
do {
char buffer[200];
int received = net_recv(buffer, 200, &endOfPhoto);
fwrite(buffer, 1, received, file);
} while (!endOfPhoto);
// Close each file after loop is complete.
fclose(file);
}
//net_send("Hello world from the server! This is a longer response that will not fit inside one frame because it is longer than one hundred and twenty four bytes.", 151, 1);
phy_close();
fclose(logfile);
return 0;
}