-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientThread.java
115 lines (98 loc) · 3.12 KB
/
ClientThread.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
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
/*
Tic Tac Toe Multiplayer
Author: E/14/009 Adikari A.M.H.I @ EFAC, University of Peradeniya
Date:30/08/2017
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
class ClientThread extends Thread {
private Thread t;
private String threadName;
private GameController game;
private String host;
private int port;
Socket client;
public static final int BASE_PORT = 1250;
private ServerSocket serverSocket;
ClientThread( GameController game,String host,int port){
this.game = game;
threadName = "clientThread";
this.host=host;
this.port=port;
}
@Override
public void run() {
try {
client_loop();
} catch (Exception e) {
}
}
public void client_loop() throws IOException {
try {
client = new Socket(host, port);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
for(line = br.readLine();
line != null && !line.equals("quit");
line = br.readLine()) {
btnaction(line);
}
} catch (IOException iOException) {
}finally {
client.close();
}
}
public void startt ()
{
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
private void btnaction(String key){
switch(key){
case "1" : if(game.contentPane.getBtn1().isEnabled()){
game.btn1ActionPerformed();
}
break;
case "2" : if(game.contentPane.getBtn2().isEnabled()){
game.btn2ActionPerformed();
}
break;
case "3" : if(game.contentPane.getBtn3().isEnabled()){
game.btn3ActionPerformed();
}
break;
case "4" : if(game.contentPane.getBtn4().isEnabled()){
game.btn4ActionPerformed();
}
break;
case "5" : if(game.contentPane.getBtn5().isEnabled()){
game.btn5ActionPerformed();
}
break;
case "6" : if(game.contentPane.getBtn6().isEnabled()){
game.btn6ActionPerformed();
}
break;
case "7" : if(game.contentPane.getBtn7().isEnabled()){
game.btn7ActionPerformed();
}
break;
case "8" : if(game.contentPane.getBtn8().isEnabled()){
game.btn8ActionPerformed();
}
break;
case "9" : if(game.contentPane.getBtn9().isEnabled()){
game.btn9ActionPerformed();
}
break;
}
}
}