-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.java
More file actions
39 lines (34 loc) · 1.25 KB
/
client.java
File metadata and controls
39 lines (34 loc) · 1.25 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
import java.io.*;
import java.net.*;
class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8000);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
new Thread(() -> {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String serverResponse = input.readLine();
System.out.println(serverResponse);
if (serverResponse.contains("win") || serverResponse.contains("lose")) {
break;
}
}
} catch (IOException e) {
System.out.println("Oops: " + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
// Oh, well!
}
}
}).start();
while (true) {
System.out.println("> ");
String command = keyboard.readLine();
output.println(command);
}
}
}