Remote Method Invocation (RMI) Overview


Java Support For Network Programming

The Java platform makes network programming easy with the classes provided in the java.net package. This section provides a brief overview of java network programming concepts and introduces you to several frequently used classes in the java.net package. These classes include ServerSocket, Socket, and URL. You’ll have a chance to apply what you learn here in chapter 20 — Client-Server Applications.

A Network Programming Scenario Using Sockets — Overview

Generally speaking, when writing a network program in Java it will be either a server application or a client application. Let’s start with a server application. Figure 19-18 shows a Java server application utilizing the ServerSocket class to listen for incoming client connections. A ServerSocket object is created and bound to a particular host IP address and port. Address and port binding can occur when the ServerSocket object is created. An example of a suitable address and port might be the localhost address (127.0.0.1) and port 15123.

image from book
Figure 19-18: Java Server Application Utilizing a ServerSocket Object

Client applications use the Socket class to establish a connection to a server application located at a specific address and port. This is illustrated in figure 19-19. When the ServerSocket detects an incoming client connection a server-side Socket is retrieved from the ServerSocket object and is used to communicate with the client as is shown in figure 19-20. For multi-threaded server applications the Socket object is passed off to a separate thread that handles communication between the server and the client. That’s why it’s important to understand Java threading concepts. (See chapter 16Threads)

image from book
Figure 19-19: Incoming Client Connection

image from book
Figure 19-20: Connection Between Client & Server Established

Once the connection between the client and server is established each side can use their respective Socket objects to send and receive data. This is accomplished by getting the OutputStream and InputStream objects from the Socket object as is shown in figure 19-21. In a multi-threaded server application, after the Socket object is passed to a separate thread for further processing, the ServerSocket object can return to the task of waiting for incoming client connections. In this way a multi-threaded server application can handle any number of simultaneous client connections.

image from book
Figure 19-21: Retrieve IOStream Objects from Server and Client Socket Objects

The URL Class

Using the ServerSocket, Socket, InputStream, and OutputStream classes is necessary if you are interested in writing client-server applications. Sometimes, however, you will simply want to access the resources provided by a network server, download the data, and manipulate it in your program. Java provides the URL and the URLConnection classes for just this purpose. This section demonstrates the use of the URL class.

The URL class represents a Uniform Resource Locator (URL) and provides methods that let you download the data referenced by the URL. The following example offers a short program that creates a URL object using the URL of a text document located on the www.warrenworks.com website and displays the text in the console.

Referring to example 19.1 — the URL object is created on line 7 using an absolute URL reference string that represents the network resource to retrieve. On line 8 a DataInputStream object is created using the URL.open-Stream() method, which returns an InputStream, in the DataInputStream constructor call.

Example 19.1: URLTestApplication.java

image from book
 1     import java.net.*; 2     import java.io.*; 3 4     public class URLTestApplication { 5       public static void main(String[] args){ 6         try{ 7            URL url = new URL("http://www.warrenworks.com/ITP_120/SampleText.txt"); 8            DataInputStream dsr = new DataInputStream(url.openStream()); 9            String s; 10           while((s = dsr.readLine()) != null){ 11             System.out.println(s); 12           } 13         }catch(Exception e){ 14            e.printStackTrace(); 15         } 16      }// end main 17     }
image from book

A String reference is declared on line 9 and used in the while loop on line 10 to store strings as they are read from the network. The results of running this program are shown in figure 19-22.

image from book
Figure 19-22: Results of Running Example 19.1

Quick Review

The Java platform makes network programming easy with the classes provided in the java.net package. Use the ServerSocket and Socket classes when writing client-server network applications. The ServerSocket class is used in a server application to listen for incoming client connections. The client uses a Socket class to initiate connections to a server located at a particular address listening on a particular port.

When the ServerSocket detects an incoming client connection a server-side Socket object can be obtained through which the server can communicate with the client using InputStreams and OutputStreams.

The URL class simplifies network programming by letting you retrieve network resources via Uniform Resource Locators.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net