Download presentation
Presentation is loading. Please wait.
1
1 L54 Networking (3)
2
2 OBJECTIVES In this chapter you will learn: To understand Java networking with URLs, sockets and datagrams. To implement Java networking applications by using sockets and datagrams. To understand how to implement Java clients and servers that communicate with one another. To understand how to implement network-based collaborative applications. To construct a multithreaded server.
3
3 24.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server Multiple threads – Server uses one thread per player Allow each player to play game independently
4
4 Outline TicTacToeServer.java (1 of 12)
5
5 Outline TicTacToeServer.java (2 of 12)
6
6 Outline TicTacToeServer.java (3 of 12) Line 50 Line 55 Create array players with 2 elements Create ServerSocket to listen on port 12345
7
7 Outline TicTacToeServer.java (4 of 12) Lines 75-87 Line 79 Line 80 Loop twice, blocking at line 79 each time while waiting for a client connection Create a new Player object to manage the connection as a separate thread Execute the Player in the runGame thread pool
8
8 Outline TicTacToeServer.java (5 of 12)
9
9 Outline TicTacToeServer.java (6 of 12)
10
10 Outline TicTacToeServer.java (7 of 12)
11
11 Outline TicTacToeServer.java (8 of 12) Lines 200-201 Get the streams to send and receive data
12
12 Outline TicTacToeServer.java (9 of 12) Lines 213-214 Line 215 Lines 225-226 Format output notifying the other player of the move Call Formatter method flush to force the output to the client Send player’s mark
13
13 Outline TicTacToeServer.java (10 of 12) Lines 231-233 Lines 254-255 Send message indicating one player connected and waiting for another player to arrive Begin the game
14
14 Outline TicTacToeServer.java (11 of 12) Lines 259-260 Line 269 Lines 272 Lines 275-276 Send message indicating player O connected Read a moveCheck the moveSend message indicating the move is valid
15
15 Outline TicTacToeServer.java (12 of 12) Lines 280-281 Send message indicating the move is invalid
16
16 Outline TicTacToeServer Test.java Program output
17
17 Outline TicTacToeClient.java (1 of 10)
18
18 Outline TicTacToeClient.java (2 of 10)
19
19 Outline TicTacToeClient.java (3 of 10)
20
20 Outline TicTacToeClient.java (4 of 10) Lines 85-86 Lines 89-90 Line 105 Connect to the serverGet the streams to send and receive data Read mark character from server
21
21 Outline TicTacToeClient.java (5 of 10) Lines 121-125 Lines 123-124 Lines 132-136 Loop continually Read and process messages from server If valid move, write message and set mark in square
22
22 Outline TicTacToeClient.java (6 of 10) Lines 137-141 Lines 142-153 If invalid move, display message If opponent moves, set mark in square
23
23 Outline TicTacToeClient.java (7 of 10)
24
24 Outline TicTacToeClient.java (8 of 10) Lines 192-193 Send the move to the server
25
25 Outline TicTacToeClient.java (9 of 10)
26
26 Outline TicTacToeClient.java (10 of 10)
27
27 Outline TicTacToeClient Test.java
28
2005 Pearson Education, Inc. All rights reserved. 28 Fig.24.17 | Sample outputs from the client/server Tic-Tac-Toe program. (Part 1 of 2.)
29
2005 Pearson Education, Inc. All rights reserved. 29 Fig.24.17 | Sample outputs from the client/server Tic-Tac-Toe program. (Part 2 of 2.)
30
30 24.9 Security and the Network By default, applets cannot perform file processing Applets often limited in machine access – Applets can communicate only with the machine from which it was originally downloaded Java Security API – Digitally signed applets – Applets given more privileges if from trusted source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.