1 / 18

Chapter 18 Networking

Chapter 18 Networking. Client/Server Communications Simple Client/Server Applications Serve Multiple Clients Create Applet Clients Send and Retrieve Ob j ects on the Network The URL Class Retrieve Files from the Network Retrieve Files from Web Servers View HTML Pages.

keita
Télécharger la présentation

Chapter 18 Networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 18 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

  2. Client/Server Communications

  3. Coding Client and Server

  4. Example 18.1 A Client/Server Example • Objective: 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 bythe server is the area of the circle.

  5. Example 18.1, cont. 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.

  6. Example 18.2 Serving Multiple Clients Server for Multiple Clients Run Note: The Run button will start the server first, then starts two clients. Press Ctrl+C to close the window.

  7. 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.

  8. Example 18.3 Creating Applet Clients Objective: shows how to use an applet to register students. The client collects and sends registration information to the server. The server appends the information to a data file using a random access file. Server Code Client Code Run Note: The Run button will start the server first, then the client applet.

  9. Example 18.4 Passing Objects in Network Programs Objective: This example rewrites Example 18.3, using object streams on the socket. Instead of passing name, street, state, and zips separately, this program passes the student object as a whole object. Server Code Client Code Run Note: The Run button will start the server first, then the client applet.

  10. Viewing HTML Pages • Given the URL of the page, a Web browser can view an HTML page—for example, http://www.sun.com. • HTTP (Hypertext Transfer Protocol) is the common standard used for communication between a Webserver and the Internet. You can open a URL andview a Web page in a Java applet. • A URL is a description of a resourcelocation on the Internet. Java provides aclass—java.net.URL—to manipulateURLs.

  11. Creating a URL Instance The following statement creates a Java URL object: try { URL location = new URL("http://www.sun.com"); } catch(MalformedURLException ex) { } ViewWebPage You have to run this program from a JDK 1.2-enabled Web browser. This applet cannot run using the Applet Viewer utility.

  12. Example 18.5 Viewing HTML Pages from Java The following figure shows the process by which an applet/application reads the files on the Web server:

  13. Example 18.6 Retrieving Remote Files • Objective: The example is similar to Example 17.4. Rather than reading the file from the local system, this example reads the file from a Web server. ViewRemoteFile Run

  14. Deploy Files on a Web Server • You need to place three files on the Web server: • ViewRemoteFile.class • ViewRemoteFile.html • in.dat • For convenience, place them in one directory.

  15. Example 18.7 Creating a Web Browser Viewing HTML Files Using the JEditorPane.JEditorPane can be used to display HTML files. WebBrowser Run Applet Viewer

  16. Distributed TicTacToe Game TicTacToeServer Run Note: The Run button will start the server first, then two clients. TicTacToeClient

  17. Distributed TicTacToe, cont.

  18. Distributed TicTacToe Game

More Related