-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHumanPlayerSend.java
51 lines (45 loc) · 1.45 KB
/
HumanPlayerSend.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
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
public class HumanPlayerSend implements Runnable {
private Socket server;
private BufferedReader input;
private BufferedWriter toServer;
public HumanPlayerSend(Socket server) {
this.server = server;
try {
input = new BufferedReader(new InputStreamReader(System.in));
toServer = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
writeToServer("human");
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
String action;
try {
while ((action = input.readLine()) != null) {
writeToServer(action);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("closing...");
System.exit(0);
}
}
public void writeToServer(String message) {
try {
System.out.println("from you: " + message);
toServer.write(message);
toServer.newLine();
toServer.flush();
} catch (SocketException e) {
System.out.println("Server unexpectedly disconnected.");
//System.exit(0);
} catch (IOException e) {
System.out.println("hmmmm");
e.printStackTrace();
}
}
}