-
Notifications
You must be signed in to change notification settings - Fork 3
/
Receiver.java
65 lines (55 loc) · 1.88 KB
/
Receiver.java
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
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
public class Receiver {
private final static int FIX_PORT = 1234;
public static void main(String[] args) throws Exception {
// check argument number
if (args.length != 2 || args[0].equals("") || args[1].equals("")) {
System.out.println("Usage: java Receiver <protocol selector> <filename>");
return;
}
// get input arguments
int protocolType = Integer.parseInt(args[0]);
String fileName = args[1];
// select available n_port and create UDP socket
int port = FIX_PORT;
DatagramSocket socket;
while(true) {
try {
socket = new DatagramSocket(port);
break;
} catch (SocketException e) {
port++;
}
}
// output recvInfo file
try {
String recvInfo = getHostName() + " " + port;
FileOutputStream out = new FileOutputStream("recvInfo");
out.write(recvInfo.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
// use appropriate protocol type to receive file data
if (protocolType == 0) {
System.out.println("GBN protocol");
GBNReceiver receiver = new GBNReceiver(socket, fileName);
receiver.start();
} else if (protocolType == 1) {
System.out.println("SR protocol");
SRReceiver receiver = new SRReceiver(socket, fileName);
receiver.start();
} else {
throw new Exception("invalid protocol type");
}
}
private static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
return "localhost";
}
}
}