-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpserver.c
147 lines (100 loc) · 3.2 KB
/
udpserver.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
#include "quick.c"
int main(int argc, char **argv)
{
string filename;
pthread_mutex_init(&rec_Q_mutex, NULL);
sem_init(&rec_full,0,0);
sem_init(&rec_empty,0,RECV_Q_LIMIT);
if (argc != 2 && argc !=3) {
printf("Arguments provided: %d\n",argc);
fprintf(stderr, "usage: %s <port_for_server>\n", argv[0]);
exit(1);
}
//portno = atoi(argv[1]);
// socket: create the socket
if (argc==3)
drop_prob=atof(argv[2]);
setup_at(atoi(argv[1]));
int rec_filesize, rec_remain_data;
srand(time(0));
/*
* recvfrom: receive a UDP datagram from a client
*/
filedata metadata=getfilename_and_size(clientaddr,clientlen);
filename=metadata.filename;
rec_filesize=metadata.filesize;
printf("filename : %s , filesize: %d \n",filename.c_str(), rec_filesize);
rec_remain_data = rec_filesize;
FILE *received_file= fopen(filename.c_str(), "ab");
init_receiver_modules(clientaddr,clientlen);
while(1)
{
if(rec_remain_data>0)
{
rec_data_node data_received;
if(rec_remain_data>BUFSIZE)
data_received=changedappRecv(BUFSIZE);
else
data_received=changedappRecv(rec_remain_data);
rec_remain_data -=data_received.bytes;
fwrite(data_received.data,1,data_received.bytes,received_file);
//printf("data RECIEVED: %s\n\n",data_received.data );
}
else
{
break;
}
}
// sleep(2);
close_instance();
printf("Thread Joined\n" );
fclose(received_file);
// sleep(5);
////
cout<<"ENTER HOST IP AND PORT"<<endl;
string ip,port;
cin>>ip;
cin>>port;
set_connection_to((char*)ip.c_str(),atoi((const char*)port.c_str()));
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);
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);
}