1 / 22

Chapter 16 Networking

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

joben
Download Presentation

Chapter 16 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 16 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 16.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 16.1, cont. Server Code Run Client Code Run Note: Run Server first, then Client. Press Ctrl+C to close the window.

  6. Example 16.2 Serving Multiple Clients Run Server Server for Multiple Clients Run Client Note: Run Server first, then Client. 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 16.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 stream. Server Code Client Code Run Click Run. Type java RegistrationServerand press Enter. Alt+Tab back to this window and click Run again. Type appletviewer registrationClient.html in the second DOS window. Display the Applet Viewer on top of the server window, and enter student information. To end the server session, press Ctrl+C. (Note: This program cannot be run from the CD.)

  9. Example 16.4 Passing Objects in Network Programs Objective: This example rewrites Example 16.5, 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 Click Run. Type java RegistrationServerUsingObjectStreamand press Enter. Alt+Tab back to this window and click Run again. Type java RegistrationClientUsingObjectStream.html in the second DOS window. Display the Applet Viewer on top of the server window, and enter student information. To end the server session, press Ctrl+C. (Note: This program cannot be run from the CD.)

  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. Viewing HTML Pages • URL Examples: • http://www.ncsa.uiuc.edu:8080/demoweb/url-primer.html • http://www.ncsa.uiuc.edu/demoweb/url-primer.html • http://java.sun.com/index.html#chapter1 • http://java.sun.com/index.html • http://java.sun.com/

  12. URL • public URL(String protocol, String host, int port, String file) throws MalformedURLException • public URL(String protocol, String host, String file) throws MalformedURLException • public URL(String url) throws MalformedURLException • public String getProtocol() • public String getHost() • public int getPort() • public String getFile() • public String getRef() • public final InputStream openStream() • protected void set(String protocol, String host, int port, String file, String ref)

  13. Creating a URL Instance The following statement creates a Java URL object: try { URL location = new URL("http://www.sun.com"); } catch(MalformedURLException e) { } ViewingWebPages Run If necessary, click the Run button to access the DOS prompt. Using a JDK 1.2-enabled Web browser. This applet cannot run using the Applet Viewer utility.

  14. Retrieving Filesfrom Web Servers The following figure shows the process by which an applet reads the files on the Web server:

  15. Example 16.6 Retrieving Remote Data Files • Objective: Compute and display student exam scores. The example is similar to Example 15.5. Rather than reading the file from the local system, this example reads the file from a Web server. RetrievingRemoteFile Run If necessary, click the Run button to access the DOS prompt. Using a Web browser (such as the HotJavaBrowser), enter the following URL: http://www.geocities.com/liangjava/RetrievingRemoteFile.html

  16. The Web Server • You need to place three files on the Web server: • RetrievingRemoteFile.class • RetrievingRemoteFile.html • in.dat • For convenience, place them in one directory.

  17. Viewing HTML Files Using the JEditorPane JEditorPane can be used to display HTML files. WebBrowse Run Applet Viewer

  18. JEditorPane • public JEditorPane() • create a new JEditorPane. • The document model is set to null • public JEditorPane(String initialPage) throws IOException • public JEditorPane(URL initialPage) throws IOException • public void addHyperlinkListener(HyperlinkListener listener)

  19. JEditorPane • public void setPage(URL url) throws IOException • public void setPage(String url) throws IOException • public URL getPage()

  20. Distributed TicTacToe Game

  21. Distributed TicTacToe Game

  22. Distributed TicTacToe Game • cd c:\department\courses\javaprog\book\ • Start sever: start java TicTacToeServer • Start 4 clients: • Start appletviewer TicTacToeClient.html

More Related