-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
354 lines (306 loc) · 11.8 KB
/
client.c
File metadata and controls
354 lines (306 loc) · 11.8 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
352
353
354
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <math.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#define PORT 5000 // port number used by server socket
#define chunksize 32 //default chunk size
int client_socket, server_addr_length;
struct sockaddr_in server;
int base = 1, current_packet = 1, window_size, total_packets, i = 0, pack_left;
pthread_t thread;
//struct for data packets
struct data_packet
{
int packet_no;
int packet_size;
int retransmitted;
// char data[33];
char data[chunksize + 1];
};
// struct for retransmitted data packets
struct ack_pending
{
int packet_size;
// char d[33];
char d[chunksize + 1];
int pack_no;
struct ack_pending *next;
};
struct ack_pending *head = NULL;
struct ack_pending *curr = NULL;
// suubstring logic for slicing the buffer containing filedata into chunks
void substring(char *str, char *substr, int pos, int size)
{
int k = 0;
while (k < size)
{
*(substr + k) = str[k + pos];
k++;
}
*(substr + size) = '\0';
//printf("Substring - %s\n", substr);
}
// function triggered if ack not received for a packet after timeout
void timeout_handler(int signum)
{
if (head != NULL)
{
struct data_packet lost_packet; // creating data_packet instance for lost packet
lost_packet.packet_no = head->pack_no;
lost_packet.packet_size = head->packet_size;
strcpy(lost_packet.data, head->d);
// filling dummy data to fill out remaining space in char array
int strlength = strlen(lost_packet.data);
if (strlength < chunksize)
{
for (int k = strlength; k < (chunksize + 1); k++)
{
lost_packet.data[k] = '|';
}
}
lost_packet.data[chunksize] = '\0';
lost_packet.retransmitted = 1; // setting the retransmitted status as 1 so that retransmitted packet isnt dropped
int retransmit = sendto(client_socket, &lost_packet, sizeof(lost_packet), 0, (struct sockaddr *)&server, server_addr_length);
if (retransmit < 0)
{
printf("Error in retransmitting packet - %d\n", lost_packet.packet_no);
}
//printf("Retransmitted packet -%d \\ data-%s\n", lost_packet.packet_no, lost_packet.data);
printf("Retransmitted packet-%d\tBase-%d\n", lost_packet.packet_no, base);
// alarm(0);
}
else
{
return;
}
}
void *receive_ack(void *arg)
{
while (pack_left > 0)
{
int recvd_packno = 0;
signal(SIGALRM, timeout_handler);
alarm(1); // starting timer for window to retransmit packet after timeout
int rec_ack = recvfrom(client_socket, &recvd_packno, sizeof(recvd_packno), 0, (struct sockaddr *)&server, &server_addr_length);
if (rec_ack < 0)
{
printf("Error in receiving ack from server\n");
}
else
{
pack_left--;
struct ack_pending *ptr = head;
struct ack_pending *prev = NULL;
while (ptr != NULL)
{
if (ptr->pack_no == recvd_packno && ptr == head)
{
if (ptr->next != NULL) //if ack received belongs to head node then change head and change base
{
// alarm(0);
head = head->next;
base = head->pack_no;
free(ptr);
break;
}
else
{
if (ptr->pack_no == total_packets) //if ack received is for last packet and only head node is present
{
// alarm(0);
head = head->next;
free(ptr);
break;
}
else //if ack received for a packet and only head node is present
{
// alarm(0);
base = ptr->pack_no + window_size;
head = head->next;
free(ptr);
break;
}
}
}
else if (ptr->pack_no == recvd_packno) //if ack received for a packet where node is present in between
{
// alarm(0);
prev->next = ptr->next;
free(ptr);
ptr = NULL;
prev = NULL;
break;
}
prev = ptr;
ptr = ptr->next;
}
printf("Received ack for packet-%d\tBase-%d\n", recvd_packno, base);
// ORIGINAL LOGIC
/*if (head == NULL && current_packet > total_packets)
{
break;
return NULL;
}*/
/*if (head == NULL && i > total_packets) // new logic
{
break;
}*/
}
}
return NULL;
}
int main()
{
client_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); // created a client socket
if (client_socket < 0)
{
printf("Error in creating client socket! \n");
exit(0);
}
printf("Client socket created successfully! \n");
printf("Enter the window size to be used\n");
scanf("%d",&window_size);
// defining server struct parameters
// struct sockaddr_in server;
server.sin_family = AF_INET; // denotes address family that the server uses
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // denotes the server's ip address
server.sin_port = htons(PORT);
server_addr_length = sizeof(server);
int ws_sent = sendto(client_socket, &window_size, sizeof(window_size), 0, (struct sockaddr *)&server, server_addr_length);
if (ws_sent < 0)
{
printf("Error in communicating window size to server! \n");
exit(0);
}
printf("Window size communicated to server successfully! \n");
char filename[10]; // storing name of the file
// double packetsize = 32; // defining default packet size
// int psize = (int)packetsize;
double packetsize = (double)chunksize; //creating double packetsize variable to count total packets further below
// packetsize = psize;
double filesize;
printf("Enter the name of the file to be read\n");
scanf("%s", filename);
FILE *file = fopen(filename, "r");
fseek(file, 0L, SEEK_END); // seeking to the end of file
filesize = ftell(file); // finding current position of file ptr
fseek(file, 0L, SEEK_SET); // resetting file pointer to initial position
char *filedata = (char *)malloc(filesize); // buffer for storing file contents
fread(filedata, filesize, 1, file); // reading entire file contents into the buffer
total_packets = (int)ceil((filesize) / packetsize); // calculating the total number of packets required to transfer the file
pack_left = total_packets;
int sent_tot_pack = sendto(client_socket, &total_packets, sizeof(total_packets), 0, (struct sockaddr *)&server, server_addr_length);
if (sent_tot_pack < 0)
{
printf("Error in communicating total packets to server !\n");
exit(0);
}
int local_base;
int pos = 0;
while (current_packet <= total_packets)
{
struct data_packet data_pack;
local_base = base;
// int run=0;
// loop to send outstanding packets for the current window
for (i = current_packet; i <= (base + window_size - 1); i++) // HERE I WAS INITIALLY LOCAL VARIABLE DECLARED IN THE FOR LOOP
{
if (i > total_packets)
break;
// for transmitting normal data packets
else
{
// char cdata[psize + 1];
char cdata[chunksize + 1];
// fetch data for packet before sending
// substring(filedata, cdata, pos, psize); // reading 32 bytes of data from file buffer sequentially
substring(filedata, cdata, pos, chunksize);
// printf("Packet %d - %s\n", i, cdata); // printing chunk of data
// creating a data packet and populating it with the data to be transferred
data_pack.packet_no = i;
// filling dummy data to fill out remaining space in char array
int strlength = strlen(cdata);
if (strlength < chunksize)
{
for (int k = strlength; k < (chunksize + 1); k++)
{
cdata[k] = '|';
}
}
strcpy(data_pack.data, cdata);
// data_pack.data[33] = '\0';
data_pack.packet_size = sizeof(data_pack);
data_pack.retransmitted = 0;
// sending packet to server
int send_pack = sendto(client_socket, &data_pack, sizeof(data_pack), 0, (struct sockaddr *)&server, server_addr_length);
if (send_pack < 0)
{
printf("Error in sending data packet-%d\n", data_pack.packet_no);
exit(0);
}
printf("Packet sent-%d \t Base - %d\n", data_pack.packet_no, base);
//printf("Data in packet -%d \\ %s\n", data_pack.packet_no, data_pack.data);
current_packet++;
// creating pending ack node in linked list
if (head == NULL)
{
head = (struct ack_pending *)malloc(sizeof(struct ack_pending));
head->next = NULL;
head->pack_no = data_pack.packet_no;
head->packet_size = data_pack.packet_size;
strcpy(head->d, data_pack.data);
// head->d[32]='\0';
curr = head;
}
else
{
struct ack_pending *new_node = (struct ack_pending *)malloc(sizeof(struct ack_pending));
new_node->next = NULL;
// new_node->ack_status = 0;
new_node->pack_no = data_pack.packet_no;
new_node->packet_size = data_pack.packet_size;
strcpy(new_node->d, data_pack.data);
// new_node->d[32]='\0';
curr->next = new_node;
curr = curr->next;
}
// pos = pos + psize;
pos = pos + chunksize;
}
}
// launching a thread to receive acks
if (base == 1)
{
// thread to receive ack continuously
int thread_err = pthread_create(&thread, NULL, receive_ack, NULL);
}
do
{
int dummy = 0;
} while (local_base == base);
}
pthread_join(thread, NULL);
fclose(file);
//printf("Outside sending loop\n");
// creating END packet
struct data_packet end_pack;
end_pack.packet_no = 0;
strcpy(end_pack.data, "END");
end_pack.packet_no = sizeof(end_pack);
end_pack.retransmitted = 1;
// sending END packet to server
int end_packet = sendto(client_socket, &end_pack, sizeof(end_pack), 0, (struct sockaddr *)&server, server_addr_length);
if (end_packet < 0)
{
printf("Error in sending data packet-%d\n", end_pack.packet_no);
exit(0);
}
printf("End Packet sent-%d\n", end_pack.packet_no);
close(client_socket);
printf("File sent successfully \n");
}