Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L22 (Chapter 25) Networking.

Similar presentations


Presentation on theme: "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L22 (Chapter 25) Networking."— Presentation transcript:

1 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L22 (Chapter 25) Networking 1

2 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2 Objectives F To comprehend socket-based communication in Java (§25.2). F To understand client/server computing (§25.2). F To implement Java networking programs using stream sockets (§25.2). F To obtain Internet address using the InetAddress class (§25.3). F To develop servers for multiple clients (§25.4). F To develop applets that communicate with the server (§25.5). F To send and receive objects on the network (§25.6). F To create applications or applets to retrieve files from the network (§25.7). F To render HTML files using the JEditorPane class (§25.8). F To implement Java networking programs using datagram sockets (§25.10 Optional).

3 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 3 Client/Server Communications The server must be running when a client starts. The server waits for a connection request from a client. To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections. After a server socket is created, the server can use this statement to listen for connections. The client issues this statement to request a connection to a server. After the server accepts the connection, communication between server and client is conducted the same as for I/O streams.

4 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 4 Data Transmission through Sockets InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream();

5 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 5 A Client/Server Example F Problem: Write a client to send data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle.

6 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 6 A Client/Server Example, cont.

7 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 7 A Client/Server Example, cont. Server Code Client Code Note: Start the server, then the client. Start Server Start Client


Download ppt "Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 L22 (Chapter 25) Networking."

Similar presentations


Ads by Google