-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.c
More file actions
351 lines (313 loc) · 9.99 KB
/
server.c
File metadata and controls
351 lines (313 loc) · 9.99 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
/**
* file: server.c
* Authors: Derek Johnson & Anjan Rayamajhi
* Created: March 21, 2014
* Revisions:
*/
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <string.h> // for memset()
#include <netinet/in.h> // for in_addr
#include <sys/socket.h> // for socket(), connect(), sendto(), and recvfrom()
#include <arpa/inet.h> // for sockaddr_in and inet_addr()
#include <stdlib.h> // for atoi() and exit()
#include <unistd.h> // for close()
#include "tcp.h"
#define INTERVAL 1
void cleanup(int ignored);
void handle_alarm(int ignored);
client* clnt = NULL; // Client info structure
int sock; // Socket
int packetsRecvd = 0; // Total # of packets recvd
double tBytesRecvd = 0.0; // Total # of bytes recvd
int lost = 0; // Total packets lost
int acks = 0; // Counts the # of acks sent
double loss_rate_sum = 0; // Used for calculating the average loss rate
int recvd = 0; // Counts # of packets properly recv'd
int start_num = 0; // Starting sequence #
double start, end;
double rx_rate = 10000; // In bits per sec
double bits_recvd = 0; // Used to calculate recv_rate
// Main function and loop for the server to recieve and keep track of
// statistics for each client.
int main(int argc, char *argv[])
{
struct sockaddr_in serv_addr; // Local address
struct sockaddr_in clnt_addr; // Client Address info
unsigned int addr_len; // Length of clnt_addr
char buf[MSGMAX]; // Buffer for echo string
unsigned short serv_port; // Server port
int numBytesRecvd; // Size of received message
short int *length = (short int*)buf; // Pointer to message LENGTH field
char *type = &(buf[2]); // Pointer to message TYPE field
char *code = &(buf[3]); // Pointer to message CODE field
int *id = (int*)(&(buf[4])); // Pointer to message CxID field
int *seq = (int*) (&(buf[8])); // Pointer to message SEQ# field
int seq_num; // Holds the host byte order seq #
short *size = (short*)(&(buf[12])); // Ptr to size field in start msg
//double *time = (double*)(&(buf[12])); // Pointer to TIMESTAMP field
int count = 0; // Counter for Loss Event
int event = 0; // Counts the # of loss events to have occurred
double loss_rate; // Estimated loss event rate
double diff; // Time measurements
double s_hat = 1000000; // Average loss interval parameter s_hat
double s_hat_new = 0; // Average loss interval parameter s_hat_new
//double s_hat_used = 1; // Average loss interval
int s[9] = {0}; // Holds all of the s(i)'s
int in_event = 0; // 1 if a loss event is occurring and 0 otherwise
double sec1, sec2;
int first_event = 0;
int k;
int last_seq_recvd;
clnt = (client *)malloc(sizeof(client));
addr_len = (unsigned int)sizeof(clnt_addr);
signal(SIGINT, cleanup);
signal(SIGALRM, handle_alarm);
// Test for correct number of parameters
if (argc != 2)
{
fprintf(stderr,"Usage: %s <Port>\n", argv[0]);
exit(1);
}
// First arg: local port
serv_port = atoi(argv[1]);
printf("Server Port:%d\n",serv_port);
printf("Size of double:%d\n", (int)sizeof(double));
printf("Time:%g\n", get_time());
// Create socket for sending/receiving datagrams
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
printf("Failure on socket call , errno:\n");
// Construct local address structure
memset(&serv_addr, 0, sizeof(serv_addr)); // Zero out structure
serv_addr.sin_family = AF_INET; // Internet address family
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface
serv_addr.sin_port = htons(serv_port); // Local port
// Bind to the local address
if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("Failure on bind, errno:\n");
}
//clnt = tcp_accept(sock);
/********ACCEPT TCP CONNECTION***********/
do
{
numBytesRecvd = recvfrom(sock, buf, CONTROL_LENGTH, 0, (struct sockaddr *) &clnt_addr, &addr_len);
if(numBytesRecvd < 0)
{
fprintf(stderr, "ERROR: Accepting Connection\n");
exit(1);
}
printf("length:%d\n", ntohs(*length));
printf("type:%d\n", (int)*type);
printf("code:%d\n", (int)*code);
printf("Cxid:%d\n", ntohl(*id));
printf("Seq#:%d\n", ntohl(*seq));
printf("size:%d\n", ntohs(*size));
}
while(*type != CONTROL && *code != START);
strcpy(clnt->ip, NET_get_ip(&clnt_addr));
clnt->port = NET_get_port(&clnt_addr);
clnt->id = ntohl(*id);
clnt->seq = ntohl(*seq);
clnt->length = ntohs(*size) + DATA_HEADER_SIZE;
printf("Client length %d\n", clnt->length);
start_num = clnt->seq + 1;
*length = ntohs(CONTROL_LENGTH);
*type = CONTROL;
*code = OK;
*id = ntohl(clnt->id);
*seq = ntohl(clnt->seq);
last_seq_recvd = clnt->seq;
sendto(sock, buf, CONTROL_LENGTH, 0, (struct sockaddr *) &(clnt_addr), sizeof(clnt_addr));
sec1 = get_time();
printf("START rcvd\n\n");
/********FINISHED ACCEPTING CLIENT*****/
start = get_time();
alarm(INTERVAL);
while(1)
{
numBytesRecvd = recvfrom(sock, buf, MSGMAX, 0, (struct sockaddr *) &clnt_addr, &addr_len);
sec2 = get_time();
/*
printf("length:%d\n", ntohs(*length));
printf("type:%d\n", (int)*type);
printf("code:%d\n", (int)*code);
printf("Cxid:%d\n", ntohl(*id));
printf("Seq#:%d\n", ntohl(*seq));
*/
if(numBytesRecvd < 0)
{
fprintf(stderr, "ERROR: Receiving Data\n");
exit(1);
}
if(strcmp(NET_get_ip(&clnt_addr), clnt->ip) != 0 || ntohl(*id) != clnt->id || NET_get_port(&clnt_addr) != clnt->port)
{
printf("Not the client\n\n");
continue;
}
/**************RESPOND TO STOP****************/
if(*type == CONTROL && *code == STOP)
{
/*send OK message*/
end = sec2;
printf("Got STOP\n\n");
*length = htons(CONTROL_LENGTH);
*type = CONTROL;
*code = OK;
*id = htonl(clnt->id);
*seq = htonl(seq_num);
sendto(sock, buf, CONTROL_LENGTH, 0, (struct sockaddr *) &clnt_addr, sizeof(clnt_addr));
/*Exit loop*/
break;
}
/*************RESPOND TO DUPLICATE START*********************/
if(*type == CONTROL && *code == START)
{
*length = ntohs(CONTROL_LENGTH);
*type = CONTROL;
*code = OK;
*id = ntohl(clnt->id);
*seq = ntohl(clnt->seq);
sendto(sock, buf, CONTROL_LENGTH, 0, (struct sockaddr *) &(clnt_addr), sizeof(clnt_addr));
printf("START recvd\n\n");
continue;
}
if(numBytesRecvd != clnt->length)
continue;
packetsRecvd++;
tBytesRecvd = tBytesRecvd + numBytesRecvd;
bits_recvd += numBytesRecvd*8;
seq_num = ntohl(*seq);
//printf("-------------------------\n");
//printf("Expected Seq #: %d\n", clnt->seq);
//printf("Last Seq #: %d\n", last_seq_recvd);
//printf("Seq# recvd: %d\n", seq_num);
//printf("# Lost: %d\n", lost);
/***************Handle Packet Loss******************/
if(*type == DATA && seq_num != (clnt->seq + 1))
{
//printf("DATA: Unexpected seq#\n\n");
//Count loss for the first time
if(seq_num > last_seq_recvd)
{
lost+= (seq_num - last_seq_recvd - 1);
}
else if(seq_num < last_seq_recvd)
{
//lost+= (seq_num - last_seq_recvd - 1);
lost--;
}
//Detect loss event
if(count < 3)
{
count++;
if(count == 3)
{
event++;
in_event = 1;
first_event++;
}
}
acks++;
diff = sec2 - sec1;
rx_rate = numBytesRecvd*8.0/diff;
last_seq_recvd = seq_num;
loss_rate_sum += ((unsigned int)floor(loss_rate*1000))/1000.0;
pack_ack(buf, clnt->id, clnt->seq, loss_rate, rx_rate, seq_num);
sec1 = get_time();
sendto(sock, buf, ACK_LENGTH, 0, (struct sockaddr *)&clnt_addr, sizeof(clnt_addr));
}
/************************Correct Packet************************/
else if(*type == DATA && seq_num == (clnt->seq + 1))
{
/*Send ACK and update clnt->seq*/
//printf("DATA: Correct seq#\n\n");
clnt->seq = seq_num;
last_seq_recvd = seq_num;
recvd++;
s[0]++;
/*Update Average loss interval*/
if(in_event)
{
//printf("In event\n");
//getchar();
if(first_event == 1)
{
for(k = 1; k <9; k++)
s[k] = s[0];
first_event++;
//printf("s[0]: %d\n", s[0]);
}
in_event = 0;
shift_s_values(s);
s_hat = calc_s_hat(&(s[1]));
//printf("s_hat: %g\n", s_hat);
}
else
{
if(seq_num < last_seq_recvd)
{
lost+= (seq_num - last_seq_recvd);
}
}
s_hat_new = calc_s_hat(s);
if(s_hat_new > s_hat)
s_hat = s_hat_new;
loss_rate = (double)(1.0/s_hat);
loss_rate_sum += ((unsigned int)floor(loss_rate*1000))/1000.0;
diff = sec2 -sec1;
rx_rate = (numBytesRecvd*8.0)/diff;
acks++;
//printf("Loss Rate: %g rx_rate: %u\n", floor(loss_rate*1000)/1000.0, (unsigned int)floor(rx_rate*1000));
pack_ack(buf, clnt->id, clnt->seq, loss_rate, rx_rate, seq_num);
sec1 = get_time();
sendto(sock, buf, ACK_LENGTH, 0, (struct sockaddr *)&clnt_addr, sizeof(clnt_addr));
count = 0;
}
else
{
/*Most likely ACK clnt->seq*/
//pack_ack(buf, clnt->id, clnt->seq, loss_rate, rx_rate, seq_num);
//sendto(sock, buf, ACK_LENGTH, 0, (struct sockaddr *)&clnt_addr, sizeof(clnt_addr));
printf("ERROR!!!!!!\n");
printf("length:%d\n", ntohs(*length));
printf("type:%d\n", (int)*type);
printf("code:%d\n", (int)*code);
printf("Cxid:%d\n", ntohl(*id));
printf("Seq#:%d\n", ntohl(*seq));
exit(1);
}
}
cleanup(1);
return 0;
}
void cleanup(int ignored)
{
printf("\n");
end = get_time();
free(clnt);
close(sock);
printf("Packets Recvd: %d Bytes Recvd: %lg\n", packetsRecvd, tBytesRecvd);
printf("Throughtput: %g\n", tBytesRecvd*8/(end-start));
printf("Acks: %d\n", acks);
//printf("Packets dropped: %d\n", lost);
//printf("Packet loss rate: %g\n", lost*1.0/(packetsRecvd+lost));
printf("Average loss event rate: %g\n", loss_rate_sum*1.0/acks);
exit(0);
return;
}
void handle_alarm(int ignored)
{
rx_rate = bits_recvd/INTERVAL;
if(rx_rate < 10000)
{
rx_rate = 10000;
}
bits_recvd = 0;
alarm(INTERVAL);
return;
}