Docsity

Networking: Client-Server Communications and File Retrieval, Slides of Computer Science

Partial preview of the text

Download Networking: Client-Server Communications and File Retrieval and more Slides Computer Science in PDF only on Docsity!

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

Client/Server Communications

Server

Server socket on port 8000 SeverSocket server = new ServerSocket(8000); A client socket Socket socket = server.accept()

Client

Client socket Socket socket = new Socket(host, serverPort#)

I/O Stream

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 by the server is the area of the circle.

Example 16.1, cont.

radius

Server Client

compute area

area

Server Code Run

Client Code

Note: Run Server first, then Client. Press Ctrl+C to close the window.

Run

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.

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.

Run

Click Run. Type java RegistrationServer and 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.)

Server Code Client Code

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 Web server and the Internet. You can open a URL and view a Web page in a Java applet.
  • A URL is a description of a resource location on the Internet. Java provides a class—java.net.URL —to manipulate URLs.

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#chapter
  • http://java.sun.com/index.html
  • http://java.sun.com/

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

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.

Run

Retrieving Files

from Web Servers

Web Server

Local file

Internet^ Web Browser^ Applet reads the file

Application reads the file

The following figure shows the process by which an applet reads the files on the Web server:

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.

Viewing HTML Files Using the

JEditorPane

JEditorPane can be used to display HTML files.

WebBrowse

Run Applet Viewer

JEditorPane

  • public void setPage(URL url)

throws IOException

  • public void setPage(String url)

throws IOException

  • public URL getPage()

Distributed TicTacToe Game

Server

Player 2

... Session N

Player 1

Session 1

... Player 1 Player 2