-
Notifications
You must be signed in to change notification settings - Fork 2
/
udp_client.c
228 lines (182 loc) · 6.11 KB
/
udp_client.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#define SERVER_PORT "4950" // the port users will be connecting to
#define BUFFER_SIZE 500 // BUFFER_SIZE as per required for each segment (e.g: 512)
//struct for data packets
struct packet {
int sequence_no;
int packet_size;
char data[BUFFER_SIZE];
};
// variables for socket
int socket_fd;
struct addrinfo serv_addr, *serv_info, *ptr; // server's address information
struct sockaddr_storage server_addr;
socklen_t server_addr_len = sizeof (struct sockaddr_storage);
int rv;
// variables for video file
int data;
int no_of_bytes;
int in;
struct stat file_stat;
int fd;
off_t file_size;
// variables for transferring data paackets and acks
struct packet packets[5]; // window size is 5
int temp_seq_no = 1;
int no_of_acks;
int temp_ack;
int acks[5];
int no_of_packets = 5;
// thread to receive acks (runs in parallel with the main program)
void* receiveAcks(void* vargp) {
// receive 5 acks
for (int i = 0; i < no_of_packets; i++) {
RECEIVE:
if((no_of_bytes = recvfrom(socket_fd, &temp_ack, sizeof(int), 0, (struct sockaddr*) &server_addr, &server_addr_len)) < 0) {
perror("UDP Client: recvfrom");
exit(1);
}
// in case of duplicate ack
if (acks[temp_ack] == 1) {
// receive ack again until a unique ack is received
goto RECEIVE;
}
// in case of unique ack
printf("Ack Received: %d\n", temp_ack);
// reorder acks according to the packet's sequence number
// make the value 1 in the acks[] array, where array position is the value of ack received (i.e. the sequence number of the packet acknowledged by the server)
acks[temp_ack] = 1;
no_of_acks++;
}
return NULL;
}
// main function
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "UDP Client: usage: Client hostname\n");
exit(1);
}
memset(&serv_addr, 0, sizeof serv_addr); // ensure the struct is empty
serv_addr.ai_family = AF_UNSPEC;
serv_addr.ai_socktype = SOCK_DGRAM; // UDP socket datagrams
if ((rv = getaddrinfo(argv[1], SERVER_PORT, &serv_addr, &serv_info)) != 0) {
fprintf(stderr, "UDP Client: getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and make a socket
for(ptr = serv_info; ptr != NULL; ptr = ptr->ai_next) {
if ((socket_fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) == -1) {
perror("UDP Client: socket");
continue;
}
break;
}
if (ptr == NULL) {
fprintf(stderr, "UDP Client: Failed to create socket\n");
return 2;
}
pthread_t thread_id; // create thread ID
// time delay variables
struct timespec time1, time2;
time1.tv_sec = 0;
time1.tv_nsec = 300000000L;
FILE * in = fopen("input_video.mp4","rb"); // open the video file in read mode
// if the file is not readable
if (in == NULL) {
perror("Error in opening the video file.\n");
return 0;
}
// size of the video file
fd = fileno(in);
fstat(fd, &file_stat);
file_size = file_stat.st_size;
printf("Size of Video File: %d bytes\n",(int) file_size);
// sending the size of the video file to the server
FILESIZE:
if(sendto(socket_fd, &file_size, sizeof(off_t), 0, ptr->ai_addr, ptr->ai_addrlen) < 0) {
// resend the file size
goto FILESIZE;
}
data = 1;
// while data left to be read
while (data > 0) {
// make packets
temp_seq_no = 0;
for (int i = 0; i < no_of_packets; i++) {
// data
data = fread(packets[i].data, 1, BUFFER_SIZE, in);
// sequence number
packets[i].sequence_no = temp_seq_no;
// packet size
packets[i].packet_size = data;
temp_seq_no++;
// last packet to be sent i.e. eof
if (data == 0){
printf("End of file reached.\n");
// Setting a condition for last packet
packets[i].packet_size = -1;
// Decrementing the remaining loops
no_of_packets = i + 1;
break;
}
}
// send 5 packets
for (int i = 0; i < no_of_packets; i++) {
printf("Sending packet %d\n", packets[i].sequence_no);
if(sendto(socket_fd, &packets[i], sizeof(struct packet), 0, ptr->ai_addr, ptr->ai_addrlen) < 0) {
perror("UDP Client: sendto");
exit(1);
}
}
// reinitialize the array
for (int i = 0; i < no_of_packets; i++) {
acks[i] = 0;
}
no_of_acks = 0;
// client starts receiving acks i.e. the thread execution starts
pthread_create(&thread_id, NULL, receiveAcks, NULL);
// wait for acks to be received i.e. the code sleeps for 0.03 seconds
nanosleep(&time1, &time2);
// selective repeat
// send those packets ONLY whose acks have not been received
RESEND:
for (int i = 0; i < no_of_packets; i++) {
// if the ack has not been received
if (acks[i] == 0) {
// sending that packet whose ack was not received
printf("Sending missing packet: %d\n",packets[i].sequence_no);
if(sendto(socket_fd, &packets[i], sizeof(struct packet), 0, ptr->ai_addr, ptr->ai_addrlen) < 0) {
perror("UDP Client: sendto");
exit(1);
}
}
}
// resend the packets again whose acks have not been received
if (no_of_acks != no_of_packets) {
// wait for acks of the packets that were resent
nanosleep(&time1, &time2);
goto RESEND;
}
// 5 acks have been received i.e. the thread executes successfully
pthread_join(thread_id, NULL);
// repeat process until the eof is not reached
}
freeaddrinfo(serv_info); // all done with this structure
printf("\nFile transfer completed successfully!\n");
close(socket_fd); // close the socket
return 0;
}