1 L54 Networking (3)
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.
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 Outline TicTacToeServer.java (1 of 12)
5 Outline TicTacToeServer.java (2 of 12)
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 Outline TicTacToeServer.java (4 of 12) Lines 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 Outline TicTacToeServer.java (5 of 12)
9 Outline TicTacToeServer.java (6 of 12)
10 Outline TicTacToeServer.java (7 of 12)
11 Outline TicTacToeServer.java (8 of 12) Lines Get the streams to send and receive data
12 Outline TicTacToeServer.java (9 of 12) Lines Line 215 Lines 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 Outline TicTacToeServer.java (10 of 12) Lines Lines Send message indicating one player connected and waiting for another player to arrive Begin the game
14 Outline TicTacToeServer.java (11 of 12) Lines Line 269 Lines 272 Lines Send message indicating player O connected Read a moveCheck the moveSend message indicating the move is valid
15 Outline TicTacToeServer.java (12 of 12) Lines Send message indicating the move is invalid
16 Outline TicTacToeServer Test.java Program output
17 Outline TicTacToeClient.java (1 of 10)
18 Outline TicTacToeClient.java (2 of 10)
19 Outline TicTacToeClient.java (3 of 10)
20 Outline TicTacToeClient.java (4 of 10) Lines Lines Line 105 Connect to the serverGet the streams to send and receive data Read mark character from server
21 Outline TicTacToeClient.java (5 of 10) Lines Lines Lines Loop continually Read and process messages from server If valid move, write message and set mark in square
22 Outline TicTacToeClient.java (6 of 10) Lines Lines If invalid move, display message If opponent moves, set mark in square
23 Outline TicTacToeClient.java (7 of 10)
24 Outline TicTacToeClient.java (8 of 10) Lines Send the move to the server
25 Outline TicTacToeClient.java (9 of 10)
26 Outline TicTacToeClient.java (10 of 10)
27 Outline TicTacToeClient Test.java
2005 Pearson Education, Inc. All rights reserved. 28 Fig | Sample outputs from the client/server Tic-Tac-Toe program. (Part 1 of 2.)
2005 Pearson Education, Inc. All rights reserved. 29 Fig | Sample outputs from the client/server Tic-Tac-Toe program. (Part 2 of 2.)
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