-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
298 lines (248 loc) · 7.64 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
/*
* udpserver.c - A simple UDP echo server
* usage: udpserver <port>
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#define BUFSIZE 1024
#define WINDOWSIZE 5120
#define MAXSEQ 30720
int recievedACK[31];
struct SendArgs {
int fd;
short seq;
int len;
char buff[1024];
const struct sockaddr *dest_addr;
socklen_t serverlen
};
void error(char *msg) {
perror(msg);
exit(1);
}
void* sendPacket(void* args) {
struct SendArgs data = *(struct SendArgs *) args;
struct timeval sent, check, result;
if (data.len == 0)
return;
char packet[1024];
packet[0] = (data.seq >> 8) & 0xFF;
packet[1] = data.seq & 0xFF;
memcpy(packet + 2, data.buff, data.len);
printf("Sending Packet seq=%d, len=%d\n", data.seq, data.len);
int n = sendto(data.fd, packet, data.len + 2, 0, data.dest_addr, data.serverlen);
if (n < 0)
error("Error sending packet.\n");
gettimeofday(&sent, NULL);
while (1) {
// If the ACK has been recieved return
if (recievedACK[data.seq/1024] == 1) {
return 0;
}
// If Timeout is Reached, Resend the Request
gettimeofday(&check, NULL);
timersub(&check, &sent, &result);
if (result.tv_usec / 1000 > 500) {
printf("Retransmitting Packet seq=%d, len=%d\n", data.seq, data.len);
int n = sendto(data.fd, packet, data.len + 2, 0, data.dest_addr, data.serverlen);
if (n < 0)
error("Error sending packet.\n");
gettimeofday(&sent, NULL);
}
}
}
int main(int argc, char **argv) {
int sockfd;
int portno;
int clientlen;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
struct hostent *hostp;
char buf[BUFSIZE];
char fileName[BUFSIZE];
char *hostaddrp;
int optval;
int n;
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
//build the server's Internet address
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
//bind: associate the parent socket with a port
if (bind(sockfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
error("ERROR on binding");
// Continuously Service Files
clientlen = sizeof(clientaddr);
while (1) {
// Init
int i;
for (i = 0; i < 31; i++)
recievedACK[i] = 1;
short windowStart = 0;
short windowEnd = windowStart + WINDOWSIZE;
long dataSent = 0;
short currSeq = 0;
int activeThreads, err;
pthread_t windowThreads[5];
struct SendArgs threadInfo[5];
// Recieve File Request
memset(fileName, 0, BUFSIZE);
n = recvfrom(sockfd, fileName, BUFSIZE, 0,
(struct sockaddr *) &clientaddr, &clientlen);
if (n < 0)
error("ERROR in recvfrom");
// Determine the Origin of the Request
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
// Open the Requested File and Read the Size
if (!isalnum(fileName[strlen(fileName) - 1]))
fileName[strlen(fileName) - 1] = '\0';
fileName[n] = '\0';
printf("fileName: %s", fileName);
FILE* file = fopen(fileName, "rb");
if (file == NULL) {
error("Error opening file");
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);
// Send the First Five Packets if Possible
for (i = 0; i < 5; i++) {
if (dataSent == fileSize) {
break; // Done sending the file
} else {
// Launch a thread to send the next segment
threadInfo[i].fd = sockfd;
threadInfo[i].len = fileSize - dataSent > 1022 ? 1022 : fileSize - dataSent;
threadInfo[i].seq = currSeq;
threadInfo[i].dest_addr = &clientaddr;
threadInfo[i].serverlen = clientlen;
memset(threadInfo[i].buff, 0, 1024);
fread(threadInfo[i].buff, sizeof(char), threadInfo[i].len, file);
recievedACK[threadInfo[i].seq/1024] = 0;
err = pthread_create(&windowThreads[i], NULL, sendPacket, &threadInfo[i]);
if (err != 0)
error("Error creating thread\n");
currSeq += threadInfo[i].len + 2;
dataSent += threadInfo[i].len;
}
}
int seq = 0;
// Send the Rest of the File While Shifting Window
while(dataSent < fileSize) {
memset(buf, 0, 1024);
n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, &clientlen);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
short ack = (((short)buf[0]) << 8) | buf[1];
printf("Recieved ACK seq=%d\n", ack);
int ackNum = (((short)buf[0]) << 8) | buf[1];
if (ackNum < 0 || ackNum > 30720)
error("Invalid ACK number");
recievedACK[ackNum/1024] = 1;
if (ackNum == windowStart) {
// Launch the New Segment(s) and Advance the Window
while (recievedACK[windowStart/1024]) {
if (dataSent == fileSize)
break;
for(i = 0; i < 5; i++) {
if (threadInfo[i].seq == windowStart) {
if (pthread_join(windowThreads[i], NULL) != 0)
printf("failed to join thread %d", windowStart);
threadInfo[i].seq = windowEnd;
threadInfo[i].len = fileSize - dataSent > 1022 ? 1022 : fileSize - dataSent;
threadInfo[i].dest_addr = &clientaddr;
threadInfo[i].serverlen = clientlen;
memset(threadInfo[i].buff, 0, 1024);
fread(threadInfo[i].buff, sizeof(char), threadInfo[i].len, file);
//printf("setting array %d to 0\n", threadInfo[i].seq/1024);
recievedACK[threadInfo[i].seq/1024] = 0;
pthread_create(&windowThreads[i], NULL, sendPacket, &threadInfo[i]);
currSeq += threadInfo[i].len + 2;
dataSent += threadInfo[i].len;
}
}
windowStart = (windowStart == 30720 ? 0 : windowStart + 1024);
windowEnd = (windowEnd == 30720 ? 0 : windowEnd + 1024);
}
}
}
// See if there are more ACKS we are waiting for
int sum = 0;
for (i = 0; i < 31; i++) {
sum += recievedACK[i];
}
// Wait for the rest of the ACKS
while (sum != 31) {
sum = 0;
memset(buf, 0, 1024);
n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, &clientlen);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
short ack = (((short)buf[0]) << 8) | buf[1];
printf("Received ACK seq=%d\n", ack);
recievedACK[ack/1024] = 1;
for (i = 0; i < 5; i++) {
if (threadInfo[i].seq == ack) {
pthread_join(windowThreads[i], NULL);
}
}
for (i = 0; i < 31; i++) {
sum += recievedACK[i];
}
}
// All ACKs Recieved, Send FIN
char packet[6];
memset(packet, 0, 6);
printf("Sending Packet FIN\nReceived Packet ACK\nReceived Packet FIN\nSending Packet ACK\n");
short seqNum = currSeq;
packet[0] = (currSeq >> 8) & 0xFF;
packet[1] = currSeq & 0xFF;
packet[2] = 'F';
packet[3] = 'I';
packet[4] = 'N';
struct timeval timeoutStart, sent, check, result;
int n = sendto(sockfd, packet, 5, 0, &clientaddr, clientlen);
gettimeofday(&timeoutStart, NULL);
gettimeofday(&sent, NULL);
while (1) {
gettimeofday(&check, NULL);
timersub(&check, &sent, &result);
if (result.tv_usec > 10) {
sendto(sockfd, packet, 5, 0, &clientaddr, clientlen);
gettimeofday(&sent, NULL);
}
timersub(&check, &timeoutStart, &result);
if (result.tv_usec > 1000)
break;
}
}
}