-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient_3.c
175 lines (134 loc) · 3.97 KB
/
Client_3.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
#include <stdio.h>
#include <stdlib.h>
// Time function, sockets, htons... file stat
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <math.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
//buffer size
#define BUFFERT 500
int create_client_socket (int port, char* ipaddr);
// Socket for Server Side
struct sockaddr_in server_socket;
// time value to store secs and mili secs
struct timeval time;
// packet struct to store sequence number and array for payload
struct packets{
int seq_number;
char payload[BUFFERT];
};
int main (int argc, char**argv){
// file descriptor AND SOCKET DISCRIPTOR
int sfd,fd;
long int size;
int no_packets;
int total_acks;
// 5 PACKETS TO SEND
struct packets packet_s0,packet_s1,packet_s2,packet_s3,packet_s4;
//buffer to store data
char buf[BUFFERT];
off_t count=0, m, file_size;
// to check if recvfrom() is successfull
long int n0,n1,n2,n3,n4;
// size of sockaddr_in
int l=sizeof(struct sockaddr_in);
struct stat buffer; // stats for file, to get size of file
if (argc != 4){
printf("Error usage : %s <ip_serv> <port_serv> <filename>\n",argv[0]);
return EXIT_FAILURE;
}
//creating the socket
sfd=create_client_socket(atoi(argv[2]), argv[1]);
if ((fd = open(argv[3],O_RDONLY))==-1){
perror("open fail");
return EXIT_FAILURE;
}
//file size
if (stat(argv[3],&buffer)==-1){
perror("stat fail");
return EXIT_FAILURE;
}
else
file_size=buffer.st_size;
// counting the total no of packets
no_packets=ceil((float)file_size/500);
//declare array of the same size
struct packets packets_array[no_packets];
//initializing ack array
int acks_recv[no_packets];
for(int a = 0; a < no_packets; a = a+1 ){
acks_recv[a]=-1;
}
//adding packets into the array
for(int var=0;var<no_packets;var++){
n0=read(fd,packets_array[var].payload,BUFFERT);}
//setting sq numbers
for(int var_1=0;var_1<no_packets;var_1++){
packets_array[var_1].seq_number=var_1;
}
total_acks = 5;
// window soze to check how many packets are sent
int pack_window=0;
// ack window to see how many acks are received
int ack_window=0;
//the sending of packets
while(pack_window!=no_packets){
total_acks=0;
for(int u=0;u<5;u++){
if(sendto(sfd,&packets_array[pack_window],sizeof(packets_array[pack_window]),0,(struct sockaddr*)&server_socket,l)==-1){
perror("send error");
return EXIT_FAILURE;}
pack_window++;
}
while(1){
recvfrom(sfd,&acks_recv[ack_window],4,0,(struct sockaddr*)&server_socket,&l);
if(acks_recv[ack_window]==ack_window){
printf("Ack Recieved %d\n",acks_recv[ack_window]);
total_acks++;
ack_window++;}
else{if(sendto(sfd,&packets_array[ack_window],sizeof(packets_array[ack_window]),0,(struct sockaddr*)&server_socket,l)==-1){
perror("send error1");
return EXIT_FAILURE;}}
if(total_acks==5){
break;}
}
}
//printf("Number of bytes transferred: %lld\n",count);
printf("On a total size of: %lld \n",file_size);
printf("For a total duration of: %ld.%d \n",delta.tv_sec,delta.tv_usec);
close(sfd);
close(fd);
return EXIT_SUCCESS;}
/* Function allowing the creation of a socket
* Returns a file descriptor
*/
int create_client_socket (int port, char* ipaddr){
int l;
int sfd;
sfd = socket(AF_INET,SOCK_DGRAM,0);
if (sfd == -1){
perror("socket fail");
return EXIT_FAILURE;
}
//preparation of the address of the destination socket
l=sizeof(struct sockaddr_in);
bzero(&server_socket,l);
server_socket.sin_family=AF_INET;
server_socket.sin_port=htons(port);
time.tv_sec = 5;
time.tv_usec = 0;
if (inet_pton(AF_INET,ipaddr,&server_socket.sin_addr)==0){
printf("Invalid IP adress\n");
return EXIT_FAILURE;
}
if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO,&time,sizeof(time)) < 0) {
perror("Error");
}
return sfd;
}