@@ -64,7 +64,7 @@ public class FileCopyClient extends Thread {
64
64
65
65
static DatagramSocket clientSocket ;
66
66
67
- static long sendbase = 1 ;
67
+ static long sendbase = 0L ;
68
68
// ... ToDo
69
69
70
70
// Constructor
@@ -75,9 +75,9 @@ public FileCopyClient(String serverArg, String sourcePathArg, String destPathArg
75
75
destPath = destPathArg ;
76
76
windowSize = Integer .parseInt (windowSizeArg );
77
77
serverErrorRate = Long .parseLong (errorRateArg );
78
- total_rtt = 0L ;
79
- received_acks = 0L ;
80
- retransmit_packs = 0L ;
78
+ total_rtt = 0L ;
79
+ received_acks = 0L ;
80
+ retransmit_packs = 0L ;
81
81
82
82
}
83
83
@@ -93,6 +93,7 @@ public void runFileCopyClient() throws IOException, InterruptedException {
93
93
DatagramPacket packet = new DatagramPacket (pinit .getSeqNumBytesAndData (), pinit .getLen () + 8 , address ,
94
94
SERVER_PORT );
95
95
clientSocket .send (packet );
96
+ sendbuffer .put (0L , pinit );
96
97
while (true ) {
97
98
while (inputstream .available () != 0 && sendbuffer .size () < windowSize ) {
98
99
inputstream .read (buf );
@@ -149,8 +150,8 @@ public void timeoutTask(long seqNum) throws IOException {
149
150
DatagramPacket pack = new DatagramPacket (fcp .getSeqNumBytesAndData (), fcp .getLen () + 8 , address , SERVER_PORT );
150
151
clientSocket .send (pack );
151
152
retransmit_packs ++;
152
- fcp .setTimestamp (System .nanoTime ());
153
- startTimer (fcp );
153
+ sendbuffer . get ( seqNum ) .setTimestamp (System .nanoTime ());
154
+ startTimer (sendbuffer . get ( seqNum ) );
154
155
}
155
156
156
157
/**
@@ -201,48 +202,53 @@ public static void main(String argv[]) throws Exception {
201
202
(new FileCopyClient (argv [0 ], argv [1 ], argv [2 ], argv [3 ], argv [4 ])).start ();
202
203
myClient .runFileCopyClient ();
203
204
}
204
-
205
- public static void ack_plus (){
205
+
206
+ public static void ack_plus () {
206
207
received_acks ++;
207
208
}
208
- public static void rtt_plus (long rtt ){
209
+
210
+ public static void rtt_plus (long rtt ) {
209
211
total_rtt = total_rtt + rtt ;
210
212
}
211
- public static void sendbase_plus (){
213
+
214
+ public static void sendbase_plus () {
212
215
sendbase ++;
213
216
}
214
-
215
-
216
217
217
218
@ Override
218
219
public void run () {
219
220
try {
220
221
byte [] data = new byte [FileCopyClient .UDP_PACKET_SIZE ];
221
222
while (true ) {
223
+
222
224
DatagramPacket udpReceivePacket = new DatagramPacket (data , data .length );
223
225
// Wait for data packet
224
226
clientSocket .receive (udpReceivePacket );
225
- rtt = System .nanoTime () - timestamp ;
226
227
FCpacket ackpack = new FCpacket (udpReceivePacket .getData (), udpReceivePacket .getLength ());
227
228
long receivedSeqNumber = ackpack .getSeqNum ();
228
- cancelTimer (sendbuffer .get (receivedSeqNumber ));
229
- long packrtt = System .nanoTime () - sendbuffer .get (receivedSeqNumber ).getTimestamp ();
230
- rtt_plus (packrtt );
231
- computeTimeoutValue (packrtt );
232
- sendbuffer .get (receivedSeqNumber ).setValidACK (true );
233
- if (sendbuffer .containsKey (receivedSeqNumber ) && receivedSeqNumber == sendbase ) {
234
- FileCopyClient .ack_plus ();
235
- sendbuffer .remove (receivedSeqNumber );
236
- FileCopyClient .sendbase_plus ();
237
- }
238
- while (sendbuffer .containsKey (sendbase ) && sendbuffer .get (sendbase ).isValidACK ()) {
239
- FileCopyClient .ack_plus ();
240
- sendbuffer .remove (receivedSeqNumber );
241
- FileCopyClient .sendbase_plus ();
229
+ System .out .println (receivedSeqNumber );
230
+ if (sendbuffer .containsKey (receivedSeqNumber )) {
231
+ cancelTimer (sendbuffer .get (receivedSeqNumber ));
232
+ long packrtt = System .nanoTime () - sendbuffer .get (receivedSeqNumber ).getTimestamp ();
233
+ System .out .println (packrtt );
234
+ rtt_plus (packrtt );
235
+ computeTimeoutValue (packrtt );
236
+ sendbuffer .get (receivedSeqNumber ).setValidACK (true );
237
+ if (receivedSeqNumber == sendbase ) {
238
+ FileCopyClient .ack_plus ();
239
+ sendbuffer .remove (receivedSeqNumber );
240
+ FileCopyClient .sendbase_plus ();
241
+ while (sendbuffer .containsKey (sendbase ) && sendbuffer .get (sendbase ).isValidACK ()) {
242
+ FileCopyClient .ack_plus ();
243
+ sendbuffer .remove (receivedSeqNumber );
244
+ FileCopyClient .sendbase_plus ();
245
+
246
+ }
247
+ }
242
248
}
243
249
}
244
250
} catch (Exception e ) {
245
- System . out . println ( e );
251
+ e . printStackTrace ( );
246
252
}
247
253
}
248
254
}
0 commit comments