-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
239 lines (197 loc) · 6.74 KB
/
client.cpp
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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
#include "packet.h"
int main(int argc, char* argv[])
{
struct sockaddr_in srv_addr; // server address
struct hostent *srv_ent; // server database
int sockfd, srv_portNum, slen = sizeof(srv_addr);
char *srv_hostname, *filename;
Packet inPacket, outPacket;
int cwnd = WINDOW_SIZE/PACKET_SIZE;
const int SIZE_MAX_SEQ = MAX_SEQUENCE_NUM/PACKET_SIZE;
if (argc != 4)
{
fprintf(stderr, "ERROR: Incorrect input. Use commands of the following format: client <server hostname> <server portnumber> <filename>\n");
exit(1);
}
srv_hostname = argv[1];
srv_portNum = atoi(argv[2]);
filename = argv[3];
// check arguments for errors
if (srv_portNum < 0)
{
fprintf(stderr, "ERROR: portnum cannot be negative\n");
exit(1);
}
// get host from database
srv_ent = (struct hostent*)gethostbyname(srv_hostname);
if (!srv_ent)
{
fprintf(stderr, "ERROR: Invalid hostname\n");
exit(1);
}
// initialize the socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
fprintf(stderr, "ERROR: A problem occured while opening the socket\n");
exit(1);
}
memset((char*)&srv_addr, 0, slen);
srv_addr.sin_family = AF_INET;
srv_addr.sin_port = htons(srv_portNum);
memcpy((char*) &srv_addr.sin_addr.s_addr, (char*) srv_ent->h_addr, srv_ent->h_length);
memset((char*)&outPacket, 0, sizeof(outPacket));
outPacket.type = REQ;
outPacket.seq = 0;
outPacket.size = strlen(filename);
memcpy(outPacket.data, filename, outPacket.size);
// while(true) {
if (sendto(sockfd, &outPacket, sizeof(outPacket), 0, (struct sockaddr*)&srv_addr, slen) == -1)
{
fprintf(stderr, "ERROR: An error occured while sending a file request\n");
exit(1);
}
printf("Sending packet SYN\n");
// if (recvfrom(sockfd, &inPacket, sizeof(inPacket), 0, (struct sockaddr *)&srv_addr, (socklen_t *)&slen) == -1) {
// fclose(fp);
// fprintf(stderr, "ERROR: A problem occured while receiving the file\n");
// exit(1);
// }
// if (select(sockfd+1, &set, NULL, NULL, &timeout) > 0) {
// }
// }
//printf("Request sent... Awaiting file\n");
FILE *fp = fopen("received.data", "w+");
if (fp == NULL)
{
fprintf(stderr, "ERROR: A problem occured while opening the file for writing\n");
exit(1);
}
memset((char*)&outPacket, 0, sizeof(outPacket));
outPacket.type = ACK;
outPacket.seq = 0;
outPacket.size = 0;
int windowBase = 0;
int shift = 0;
int seqInWindow = 0;
int pcktsReceived[cwnd];
memset((char*)&pcktsReceived, 0, cwnd*sizeof(int));
char windowBuffer[cwnd * DATA_SIZE];
memset((char*)&windowBuffer, 0, cwnd*DATA_SIZE);
char tempDataBuffer[DATA_SIZE];
memset((char*)&tempDataBuffer, 0, DATA_SIZE);
while (true) {
//fprintf(stderr, "IN WHILE LOOP!\n");
memset((char*)&inPacket, 0, sizeof(inPacket));
if (recvfrom(sockfd, &inPacket, sizeof(inPacket), 0, (struct sockaddr *)&srv_addr, (socklen_t *)&slen) == -1) {
fclose(fp);
fprintf(stderr, "ERROR: A problem occured while receiving the file\n");
exit(1);
}
else if (inPacket.seq == 0 && inPacket.type == FIN) { // file does not exist
//fprintf(stderr, "RECEIVED PACKET <3!\n");
fclose(fp);
fprintf(stderr, "ERROR: No such file exists\n");
exit(1);
}
else if (inPacket.type == DATA){
printf("Receiving packet %d\n",
(inPacket.seq* DATA_SIZE));
outPacket.seq = inPacket.seq;
seqInWindow = inPacket.seq - (windowBase % SIZE_MAX_SEQ);
// recieved an DATA packet from previous window:
if ( ( ((windowBase)%SIZE_MAX_SEQ > (windowBase - cwnd) %SIZE_MAX_SEQ) && (inPacket.seq >= (windowBase - cwnd)%SIZE_MAX_SEQ && inPacket.seq < (windowBase)%SIZE_MAX_SEQ) ) ||
( ((windowBase)%SIZE_MAX_SEQ < (windowBase-cwnd) % SIZE_MAX_SEQ) && !( inPacket.seq < (windowBase-cwnd)%SIZE_MAX_SEQ && inPacket.seq >= (windowBase)%SIZE_MAX_SEQ) )
) {
// just send ACK, below
}
else if ( ( ((windowBase+cwnd)%SIZE_MAX_SEQ > windowBase%SIZE_MAX_SEQ) && (inPacket.seq >= windowBase%SIZE_MAX_SEQ && inPacket.seq < (windowBase + cwnd)%SIZE_MAX_SEQ) ) ||
( ((windowBase+cwnd)%SIZE_MAX_SEQ < windowBase % SIZE_MAX_SEQ) && !( inPacket.seq < windowBase%SIZE_MAX_SEQ && inPacket.seq >= (windowBase+cwnd)%SIZE_MAX_SEQ) )) {
//fprintf(stderr, "IN IF STATEMENT THO\n\n");
//fprintf(stderr, "\nHERE I AM!\n inPacket.seq = %d\n windowBase = %d\n", inPacket.seq, windowBase);
if (inPacket.seq == windowBase % SIZE_MAX_SEQ) {
fwrite(inPacket.data, sizeof(char), inPacket.size, fp);
// go through buffer and deliver any buffered packets, in order
//printf("WROTE TO FILE: packet number %d\n\n", windowBase);
shift = 1;
int i = 1;
int j = 0;
for (; i < cwnd; i++) {
if (pcktsReceived[i] == 0) {
break;
}
else {
fwrite(windowBuffer + (DATA_SIZE*i), sizeof(char), DATA_SIZE, fp);
shift++;
//printf("WROTE TO FILE: packet number %d\n\n", windowBase+i);
}
}
// advance windowBase to next not-yet-recvd packet
for (; j < (cwnd-shift); j++) {
pcktsReceived[j] = pcktsReceived[shift+j];
memcpy(tempDataBuffer, windowBuffer+(DATA_SIZE*(j+shift)), DATA_SIZE);
memcpy(windowBuffer + (DATA_SIZE*j), tempDataBuffer, DATA_SIZE);
}
// fill in rest of buffers as empty
for (; j < cwnd; j++) {
pcktsReceived[j] = 0;
memset(windowBuffer + (DATA_SIZE*j), 0, DATA_SIZE);
}
windowBase = windowBase + shift;
}
else {
// add packet to buffer, in order
memcpy(windowBuffer + (DATA_SIZE*seqInWindow), inPacket.data, inPacket.size);
pcktsReceived[seqInWindow] = 1;
}
}
// else {
// continue;
// }
}
else {
//fprintf(stderr, "RECEIVED PACKET <3!\n");
if (inPacket.type == FIN) {
printf("Receiving packet %d FIN\n",
(inPacket.seq* DATA_SIZE));
break;
}
else {
printf("WARNING: Received non-data packet. Ignored\n");
continue;
}
}
//fprintf(stderr, "RECEIVED PACKET!\n");
if (sendto(sockfd, &outPacket, sizeof(outPacket), 0, (struct sockaddr *)&srv_addr, slen) == -1)
{
fprintf(stderr, "ERROR: A problem occured while sending an ACK packet\n");
exit(1);
}
printf("Sending packet %d\n",
(outPacket.seq * DATA_SIZE));
}
// send FIN ACK
outPacket.type = FIN;
outPacket.seq = inPacket.seq;
outPacket.size = 0;
if (sendto(sockfd, &outPacket, sizeof(outPacket), 0, (struct sockaddr *)&srv_addr, slen) == -1)
{
fprintf(stderr, "ERROR: A provlem occured while sending FIN_ACK packet\n");
exit(1);
}
printf("Sending packet %d FIN_ACK\n",
(outPacket.seq* DATA_SIZE));
//printf("Connection closed\n");
fclose(fp);
close(sockfd);
return 0;
}