-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.c
executable file
·243 lines (198 loc) · 4.61 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
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <stdbool.h>
#include <pthread.h>
#define VIDEO_FILE "testFiles/outUDP.mp4"
#define PORT 8080
#define BUFFER_SIZE 500
#define WINDOW_SIZE 5
struct Packet {
int seqNum;
int size;
char data[BUFFER_SIZE];
bool sent;
bool received;
bool ack;
bool eof;
};
struct ackPacket
{
int seqNum;
int size;
bool eof;
};
int num_of_pkt_wait_for = 0;
int num_of_acked_pkts = 0;
int n;
int udpSocket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE];
FILE *output_file;
int seq_num = 0;
struct ackPacket ack;
int last_acked_in_sequence;
bool eof_reached;
bool eof_reached = false;
struct Packet window[WINDOW_SIZE];
void rescOverR_Udp();
void recvOverUDP();
void threadSendAck();
void initWindowSeq();
void slideWindow(int steps);
void addPacket();
void socketConnection() {
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpSocket < 0) {
perror("[-]socket failed");
exit(EXIT_FAILURE);
}
else {
puts("[+]Socket created");
}
// Client config
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Binding
if (bind(udpSocket, (struct sockaddr *)&address, sizeof(address))<0) {
perror("[-]bind failed");
exit(EXIT_FAILURE);
}
}
/**
* operations to receive udp reliably
*/
void rescOverR_Udp() {
num_of_pkt_wait_for = WINDOW_SIZE;
initWindowSeq();
rescAgain:
recvOverUDP();
// if all pkts acked else send acks
if (num_of_acked_pkts < WINDOW_SIZE-1) {
num_of_pkt_wait_for = WINDOW_SIZE - num_of_acked_pkts;
goto rescAgain;
}
// if eof not reached then continue
if (eof_reached == false) {
slideWindow(WINDOW_SIZE);
rescOverR_Udp();
}
// eof reached
else {
slideWindow(num_of_pkt_wait_for+1);
}
}
/**
* init a pkt array to be received
*/
void initWindowSeq() {
seq_num = 0;
for (int i = 0; i < WINDOW_SIZE; i++) {
struct Packet newPkt;
newPkt.seqNum = seq_num;
newPkt.received = false;
seq_num++;
window[i] = newPkt;
}
}
/**
* send the acks for the packets
* that are received accordingly
* --> ignore the duplicate packets
*/
void recvOverUDP() {
for (int i = 0; i < num_of_pkt_wait_for; i++) {
struct Packet newPacket;
newPacket.eof = false;
n = recvfrom(udpSocket, &newPacket, sizeof(newPacket), MSG_WAITALL, ( struct sockaddr *) &address, &addrlen);
int num = newPacket.seqNum;
// if the pkt is already recieved then ignore it
if (!window[num].received)
{
printf("packet received: %d\n", num);
window[num] = newPacket;
window[num].received = true;
struct ackPacket ack;
ack.seqNum = num;
ack.eof = newPacket.eof;
int n = sendto(udpSocket, &ack, sizeof(ack), MSG_CONFIRM, (const struct sockaddr *) &address, addrlen);
window[num].ack = true;
printf("Ack sent: %d\n",num);
num_of_acked_pkts++;
}
// send ack for duplicate packets
else if (window[num].received) {
struct ackPacket ack;
ack.seqNum = num;
ack.eof = newPacket.eof;
int n = sendto(udpSocket, &ack, sizeof(ack), MSG_CONFIRM, (const struct sockaddr *) &address, addrlen);
}
if (newPacket.eof == true) {
printf("eof seq num %d\n", newPacket.seqNum);
eof_reached = true;
num_of_pkt_wait_for = newPacket.seqNum;
break;
}
if (newPacket.size < 0) // Error
perror("[-]ERROR writing to socket");
}
}
/**
* pushing a new packet to the window arr
*/
void addPacket() {
/* shifting array elements */
n = WINDOW_SIZE;
for(int i=0; i < n-1; i++){
window[i] = window[i+1];
}
struct Packet nullPkt;
nullPkt.seqNum = -2;
window[n-1] = nullPkt;
}
/**
* slides the window
* --> init with new packets
* --> write down the previous packets on the file
*/
void slideWindow(int steps) {
for (int i = 0; i < steps; i++) {
struct Packet packet = window[i];
printf("writting: %d\n", packet.seqNum);
size_t num_write = fwrite(&packet.data, 1, packet.size, output_file);
if (num_write == 0) {
packet.eof = true;
break;
}
}
for (int i = 0; i < WINDOW_SIZE; i++) {
addPacket();
}
}
/**
*
* TRANSFER FILE OVER UDP
*
*/
int main(int argc, char const *argv[])
{
puts("\n");
puts("USING UDP CONNECTION");
socketConnection();
puts("[+]Client found");
puts("[+]Receiving file from the clinet.");
output_file = fopen(VIDEO_FILE, "wb");
rescOverR_Udp();
puts("[+]File is received and saved in outUDP.mp3.");
if (output_file != NULL)
fclose(output_file);
close(udpSocket);
puts("[+]Closing Connection.");
return 0;
}