-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiver1b.java
More file actions
82 lines (63 loc) · 2.09 KB
/
Receiver1b.java
File metadata and controls
82 lines (63 loc) · 2.09 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
import java.io.*;
import java.net.*;
import java.util.Arrays;
public class Receiver1b {
public static void main(String[] args) throws Exception
{
String port_number = args[0];
String file_name = args[1];
DatagramSocket serverSocket = new DatagramSocket(Integer.parseInt(port_number));
byte[] receiveData = new byte[1027];
byte[] sendData = new byte[] {0,1};
while(true) {
//recieving data from the client
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
byte[] saved_pack = new byte[receivePacket.getLength()-3];
for (int i = 0; i<receivePacket.getLength()-3; i++){
saved_pack[i] = receivePacket.getData()[i+3];
}
// configuring the IPAdress and port of the client
InetAddress IPAddress = receivePacket.getAddress();
//int port = receivePacket.getPort();
//send the sequence number as ACK packet
byte[] ack = new byte[] {receivePacket.getData()[0], receivePacket.getData()[1]};
DatagramPacket sendPacket = new DatagramPacket(ack, ack.length, IPAddress, receivePacket.getPort());
serverSocket.send(sendPacket);
//check if the current ACK matches with the previous one, if so then don't write it to the file.
if(Arrays.equals(sendData, ack)) {
continue;
}
else {
//the last ack will be the new ack
sendData = ack;
//remove the sequence count and EoF from the recieved bytes
//Open a file and write onto it
File file = null;
FileOutputStream fileOutputStream = null;
try {
file = new File(file_name);
fileOutputStream = new FileOutputStream(file, true);
//create file if not exists
if (!file.exists()) {
file.createNewFile();
}
fileOutputStream.write(saved_pack);
fileOutputStream.flush();
fileOutputStream.close();
//System.out.println("File written successfully.");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}
}