-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpclient.c
81 lines (58 loc) · 1.73 KB
/
udpclient.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
#include "quick.c"
int main(int argc, char **argv)
{
int read_count=0;
char buf[BUFSIZE];
int retransmitted = 0;
/* check command line arguments */
if (argc != 3)
{
fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
exit(0);
}
set_connection_to(argv[1],atoi(argv[2]));
printf("Enter file name\n" );
string filename_str;
cin>>filename_str;
int filesize=send_filename_and_size(filename_str,serveraddr,serverlen);
FILE* fp = fopen(filename_str.c_str(), "rb");
int ack_seq_num=0, to_trans=(filesize/1016)+1;
int remain_data = filesize,sent_bytes,nread,dont_read=0;
init_send_modules(filesize,serveraddr,serverlen);
init_receiver_modules(serveraddr,serverlen);
unsigned char packet_buf[2*BUFSIZE-8]={0};
while(1)
{
while(dont_read!=1)
{
memset(packet_buf,0,sizeof(packet_buf));
nread = fread(packet_buf,1,2*BUFSIZE-8,fp);
read_count++;
if(nread<=0)
{
dont_read=1;
break;
}
remain_data-=nread;
app_send(packet_buf,nread);
}
if (remain_data==0)
{
printf("Finished reading the file\n");
if (feof(fp))
{
printf("File Sent\n");
printf("Number of retransmitted packets = %d\n",retransmitted);
}
if (ferror(fp))
printf("Error Sending file\n");
break;
}
}
fclose(fp);
wait_till_data_sent();
close_instance();
close(sockfd);
//////////////
return 0;
}