-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.java
50 lines (49 loc) · 1.44 KB
/
Client.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client implements Runnable {
final String host;
final int port;
final String message;
boolean timeout = false;
Client(String h, int p, String o) {
host = h;
port = p;
message = o;
}
Client(String h, int p, String o, boolean t) {
host = h;
port = p;
message = o;
timeout = t;
}
String result = null;
String getResult() {
return result;
}
public void run() {
try (
Socket s = new Socket(host, port);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
) {
out.println(message);
for (;;) {
result = in.readLine();
if (result.startsWith("RESPONSE: ")) out.println(":ACK");
break;
}
} catch (IOException e) {
System.err.println(e);
}
}
public static void main(String[] args) throws InterruptedException {
Client c = new Client(args[0], Integer.parseInt(args[1]), args[2]);
Thread t = new Thread(c);
t.start();
t.join();
System.out.println(c.getResult());
}
}