-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClient.java
More file actions
141 lines (119 loc) · 3.95 KB
/
Client.java
File metadata and controls
141 lines (119 loc) · 3.95 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
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
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
public class Client {
public static final String IP_ADDR = "ec2-34-210-250-69.us-west-2.compute.amazonaws.com"; // Server Address
public static final int PORT = 12345; // General Server Port
public static int MY_PORT = -1; // Unique Server Port
public static int CLIENT_ID = 1;
private static final String header = "$$Hello! I am Client ";
public static void main(String[] args) {
System.out.println("Client is starting...");
if (args.length > 0) {
CLIENT_ID = Integer.parseInt(args[0]);
}
System.out.println("Client ID is" + CLIENT_ID);
Socket firstTrial = null;
try {
firstTrial = new Socket(IP_ADDR, PORT);
DataOutputStream out = new DataOutputStream(firstTrial.getOutputStream());
String content = header + Integer.toString(CLIENT_ID);
out.writeUTF(content);
DataInputStream input = new DataInputStream(firstTrial.getInputStream());
String ret = input.readUTF();
input.close();
out.close();
int new_port = Integer.parseInt(ret);
if (new_port < 12345) {
System.out.println("Error in Client First Trial of sending message, receieved an invalid new port");
System.exit(-1);
}
MY_PORT = new_port;
} catch (Exception e) {
System.out.println("Error in Client First Trial of sending message: " + e.getMessage());
System.exit(-1);
} finally {
if (firstTrial != null) {
try {
firstTrial.close();
} catch (IOException e) {
firstTrial = null;
System.out.println("Error in First Trial Finally: " + e.getMessage());
}
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Socket secondTrial = null;
try {
System.out.println(MY_PORT);
secondTrial = new Socket(IP_ADDR, MY_PORT);
DataOutputStream out = new DataOutputStream(secondTrial.getOutputStream());
DataInputStream input = new DataInputStream(secondTrial.getInputStream());
out.writeUTF("&Hello!&");
String rtn = null;
while (rtn == null) {
rtn = input.readUTF();
}
System.out.println(rtn);
secondTrial.close();
input.close();
out.close();
} catch (Exception e) {
System.out.println("Error in Client Main running: " + e.getMessage());
System.exit(-1);
} finally {
if (secondTrial != null) {
try {
secondTrial.close();
} catch (IOException e) {
secondTrial = null;
System.out.println("Error in Second Trial Finally: " + e.getMessage());
}
}
}
// Mimic to do sth
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Socket thirdTrial = null;
String output = "123";
try {
thirdTrial = new Socket(IP_ADDR, MY_PORT);
DataOutputStream out = new DataOutputStream(thirdTrial.getOutputStream());
DataInputStream input = new DataInputStream(thirdTrial.getInputStream());
out.writeUTF("&&DONE!" + output);
String rtn = null;
while (rtn == null) {
rtn = input.readUTF();
}
System.out.println(rtn);
if (rtn.equals("Thank you!")) {
System.out.println("Achieved Here!");
}
input.close();
out.close();
} catch (Exception e) {
System.out.println("Error in Client Third Trial: " + e.getMessage());
System.exit(-1);
} finally {
if (thirdTrial != null) {
try {
thirdTrial.close();
} catch (Exception e) {
thirdTrial = null;
System.out.println("Error in Third Trial Finally: " + e.getMessage());
}
}
}
}
}