Java Reference
In-Depth Information
29 Platform.runLater(() -> taLog.appendText(
new
Date() +
30
": Server started at socket 8000\n"
));
31
32
// Ready to create a session for every two players
33
while
(
true
) {
34 Platform.runLater(() -> taLog.appendText(
new
Date() +
35
": Wait for players to join session "
+ sessionNo +
'\n'
));
36
37
// Connect to player 1
38
Socket player1 = serverSocket.accept();
connect to client
39
40 Platform.runLater(() -> {
41 taLog.appendText(
new
Date() +
": Player 1 joined session "
42 + sessionNo +
'\n'
);
43 taLog.appendText(
"Player 1's IP address"
+
44 player1.getInetAddress().getHostAddress() +
'\n'
);
45 });
46
47
// Notify that the player is Player 1
to player1
48
new
DataOutputStream(
49
player1.getOutputStream()).writeInt(PLAYER1);
50
51
// Connect to player 2
52 Socket player2 = serverSocket.accept();
53
54 Platform.runLater(() -> {
55 taLog.appendText(
new
Date() +
56
": Player 2 joined session "
+ sessionNo +
'\n'
);
57 taLog.appendText(
"Player 2's IP address"
+
58
connect to client
player2.getInetAddress().getHostAddress() +
'\n'
);
59 });
60
61
// Notify that the player is Player 2
62
new
DataOutputStream(
63 player2.getOutputStream()).writeInt(PLAYER2);
64
65
// Display this session and increment session number
66 Platform.runLater(() ->
67 taLog.appendText(
new
Date() +
68
to player2
": Start a thread for session "
+ sessionNo++ +
'\n'
));
69
70
// Launch a new thread for this session of two players
a session for two players
71
new
Thread(
new
HandleASession(player1, player2)).start();
72 }
73 }
74
catch
(IOException ex) {
75 ex.printStackTrace();
76 }
77 }).start();
78 }
79
80
// Define the thread class for handling a new session for two players
81
class
HandleASession
implements
Runnable, TicTacToeConstants {
82
private
Socket player1;
83
private
Socket player2;
84
85
// Create and initialize cells
86
private char
[][] cell =
new char
[
3
][
3
];
87
88
private
DataInputStream fromPlayer1;
Search WWH ::
Custom Search