Networking
Networking
Client/Server Communications
Simple Client/Server Applications
Serve Multiple Clients
Create Applet Clients
Send and Retrieve Objects on the Network
The URL Class
Retrieve Files from the Network
Retrieve Files from Web Servers
View HTML Pages
Client/Server Communications
Server Host
A client socket
Socket socket =
server.accept()
Server socket on port 8000
SeverSocket server =
new ServerSocket(8000);
Client
I/O Stream
Client socket
Socket socket =
new Socket(host, serverPort#)
Server Code
Client Code
Run
Note: The Run button will start the server first, then start the client.
Press Ctrl+C to close the window.
A socket for a
client
Run
Client 1
A serve socket
on a port
...
Note: The Run button will start the server first, then starts two clients.
Press Ctrl+C to close the window.
A socket for a
client
Client n
Applet Clients
Due to security constraints, applets can only connect to
the host from which they were loaded. Therefore, the
HTML file must be located on the machine on which
the server is running.
Run
Run
Note: The Run button will start the server
first, then the client applet.
ViewWebPage
You have to run this program from a JDK 1.2-enabled Web
browser. This applet cannot run using the Applet Viewer utility.
Run
Example18.7Creatinga
WebBrowser
Viewing HTML Files Using the JEditorPane.
JEditorPane can be used to display HTML
files.
WebBrowser
Run Applet Viewer
Player 1
Player 2
...
...
Session N
Player 1
Player 2
TicTacToeServer
Run
TicTacToeClient
TicTacToeServer
-
HandleASession
-
TicTacToeConstants
+getToke
-char token n
+setToke
JApplet
+getToken n
+setToken +paintCo
-char
+paintComponet
mponet
token
+mouseClicked
Runnable
+mouseC
licked
+getToke
-char token n
+setToke
+getToken
n
TicTacToeServer
+setToken +paintCo
+paintComponet
mponet void
+main(args: String[]):
+mouseClicked
+mouseC
licked
TicTacToeConstants
+PLAYER1=1: int
+PLAYER2 = 2: int
+PLAYER1_WON = 1: int
+PLAYER2_WON = 2: int
+DRAW = 3: int
+CONTINUE = 4: int
Similar as in
Example 12.7
TicTacToeClient
-
Cell
-
HandleASession
-player1: Socket
-player2: Socket
-cell char[][]
-continueToPlay: boolean
+run(): void
-isWon(): boolean
-isFull(): boolean
-sendMove(out:
DataOuputStream, row: int,
column: int): void
TicTacToeClient
-myTurn: boolean
-myToken: char
-otherToken: char
-cell: Cell[][]
-continueToPlay: boolean
-rowSelected: int
-columnSelected: int
-isFromServer: DataInputStream
-osToServer: DataOutputStream
-waiting: boolean
+run(): void
-connectToServer(): void
-recieveMove(): void
-sendMove(): void
-receiveInfoFromServer(): void
-waitForPlayerAction(): void
Server
Accept connection from the first player and notify the player
is Player 1 with token X.
Accept connection from the second player and notify the
player is Player 2 with token O. Start a thread for the
session.
Player 2
1. Initialize user interface.
Handle a session:
3. Get the start signal from the server.
4. Wait for the player to mark a cell, send
the cell's row and column index to the
server.
5. Receive status from the server.
6. If WIN, display the winner; if player 2
wins, receive the last move from player 2.
Break the loop
7. If DRAW, display game is over; break
the loop.