Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 L52 Networking (1). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.

Similar presentations


Presentation on theme: "1 L52 Networking (1). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java."— Presentation transcript:

1 1 L52 Networking (1)

2 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java networking applications by using sockets and datagrams.  To understand how to implement Java clients and servers that communicate with one another.  To understand how to implement network-based collaborative applications.  To construct a multithreaded server.

3 3 24.1 Introduction Networking package is java.net – Stream-based communications Applications view networking as streams of data Connection-based protocol Uses TCP (Transmission Control Protocol – Packet-based communications Individual packets transmitted Connectionless service Uses UDP (User Datagram Protocol)

4 4 24.1 Introduction (Cont.) Client-server relationship – Client requests some action be performed – Server performs the action and responds to client – Request-response model Common implementation: Web browsers and Web servers

5 5 24.2 Manipulating URLs HyperText Transfer Protocol (HTTP) – Uses URIs (Uniform Resource Identifiers) to identify data URLs (Uniform Resource Locators) – URIs that specify the locations of documents – Refer to files, directories and complex objects HTML document SiteSelector.html (Fig. 24.1) – applet element – param tag name attribute value attribute

6 6 Outline Fig.24.17 | HTML document to load SiteSelector applet. Declare param tags for the applet, each parameter has a name and a value SiteSelector.html Lines 5-12

7 7 Outline SiteSelector.java (1 of 5) Lines 3-4 Line 8 Import classes in package java.net Import interface AppletContext from package java.net

8 8 Outline SiteSelector.java (2 of 5) Lines 39-52 Line 45 Line 48 Line 51 When the user selects one of the Web sites listed in siteChooser, the program calls method valueChanged Pass the selected site name (the key) to HashMap method get, which locates and returns a reference to the corresponding URL (the value) Get a reference to an AppletContext object that represents the applet container Display in the current browser window the resource associated with URL newDocument

9 9 Outline SiteSelector.java (3 of 5) Line 67 Line 73 Line 77 Lines 81-84 Use Applet method getParameter to obtain a Web site title Use Applet method getParameter to obtain the Web site location Use the location as the value of a new URL object If the location passed to the URL constructor is invalid, the URL constructor throws a MalformedURLException

10 10 Outline SiteSelector.java (4 of 5) Line 87 Program output Use Applet method getParameter to obtain the next site title

11 11 Outline SiteSelector.java (5 of 5) Program output

12 12 24.2 Manipulating URLs HTML frames – Specify target frame in method showDocument _blank _self _top

13 13 24.3 Reading a File on a Web Server Swing GUI component JEditorPane – Render both plain text and HTML-formatted text – Act as a simple Web browser Retrieves files from a Web server at a given URI HyperlinkEvent s – Occur when the user clicks a hyperlink – Three event types HyperlinkEvent.EventType.ACTIVATED HyperlinkEvent.EventType.ENTERED HyperlinkEvent.EventType.EXITED

14 14 Outline ReadServerFile.java (1 of 3) Lines 7, 12 and 13 Line 18 Import JEditPane from package javax.swing, import HyperlinkEvent and HyperlinkListener from package javax.swing.event Declare JEditorPane contentsArea, which will be used to display the contents of the file

15 15 Outline ReadServerFile.java (2 of 3) Line 40 Line 41 Lines 42-53 Lines 46-51 Lines 48-49 Line 50 Create JEditorPane using the no-argument constructor Set JEditorPane editable property to false Register a HyperlinkListener to handle HyperlinkEvents, which occur when the user clicks a hyperlink in the JEditorPane Method hyperlinkUpdate is called when a HyperlinkEvent occurs Use HyperlinkEvent method getEventType to determine the type of the HyperlinkEvent Use HyperlinkEvent method getURL to obtain the URL represented by the hyperlink

16 16 Outline ReadServerFile.java (3 of 3) Line 65 Invoke JEditorPane method setPage to download the document specified by location and display it in the JEditPane

17 17 Outline ReadServerFileTest.java (1 of 2) Program output

18 18 Outline ReadServerFileTest.java (2 of 2) Program output

19 19 24.4 Establishing a Simple Server Using Stream Sockets Five steps to create a simple server in Java – Step 1: Create ServerSocket object ServerSocket server = new ServerSocket( portNumber, queueLength ); Register an available port Specify a maximum number of clients Handshake point Binding the server to the port – Only one client can be bound to a specific port

20 20 24.4 Establishing a Simple Server Using Stream Sockets (Cont.) Five steps to create a simple server in Java – Step 2: Server listens for client connection Server blocks until client connects Socket connection = server.accept(); – Step 3: Sending and receiving data OutputStream to send and InputStream to receive data – Method getOutputStream returns Socket ’s OutputStream – Methods getInputstream returns Socket ’s InputStream

21 21 24.4 Establishing a Simple Server Using Stream Sockets (Cont.) Five steps to create a simple server in Java – Step 4: Process phase Server and Client communicate via streams – Step 5: Close streams and connections Method close

22 22 24.5 Establishing a Simple Client Using Stream Sockets Four steps to create a simple client in Java – Step 1: Create a Socket to connect to server Socket connection = new Socket ( serverAddress, port ); – Step 2: Obtain Socket ’s InputStream and Outputstream – Step 3: Process information communicated – Step 4: Close streams and connection


Download ppt "1 L52 Networking (1). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java."

Similar presentations


Ads by Google