java programming: advanced topics 1 networking programming chapter 11
TRANSCRIPT
Java Programming: Advanced Topics
1
Networking Programming
Chapter 11
Java Programming: Advanced Topics
2
Objectives
• Perform network I/O using the core classes in the package java.net
• Write programs that establish connections to URLs and perform stream I/O between URLs
• Read and write data to local and remote sites using TCP/IP sockets and datagram sockets
Java Programming: Advanced Topics
3
Objectives (Cont.)
• Use Remote Method Invocation (RMI) to develop classes of objects that are accessible from a server
• Learn the steps involved in compiling and deploying an application that uses RMI
Java Programming: Advanced Topics
4
Objectives (Cont.)
• Be introduced to the Java Naming and Directory Interface (JNDI)
• Discover the relationship between CORBA, RMI-IIOP, and the Java Interface Definition Language (IDL)
Java Programming: Advanced Topics
5
Introduction
• The Java platform includes a package java.net to perform input and output (I/O) operations over the network
• Core classes of the java.net package support URLs, TCP/IP sockets, or datagram sockets
Java Programming: Advanced Topics
6
Working with URLs
• URLs begin with a protocol specification followed by a colon and two forward slashes (://) and the host name, along with optional file and port information
• To exchange information with a URL, you must create a URLConnection by calling the method openConnection for a URL
Java Programming: Advanced Topics
7
The Use of the URL and URLConnection Objects
Java Programming: Advanced Topics
8
The Use of the URL and URLConnection Objects (Cont.)
Java Programming: Advanced Topics
9
Working with URLs (Cont.)
• Uniform Resource Identifier (URI) technology has become commonly used in technical specifications in place of the term URL
• The class java.net.URI contains a method, toURL, which can be used to construct a URL object from a URI object
Java Programming: Advanced Topics
10
Working with Sockets
• Socket: an abstraction of the end points of connections between processes or applications
• The Java platform supports two types of sockets:– TCP/IP– datagram
Java Programming: Advanced Topics
11
TCP/IP Sockets
• A TCP/IP socket is connection-oriented• Create a Socket object, ask the socket for its
input stream and output stream• To perform stream I/O to and from a TCP/IP
socket, use the methods Socket.getInputStream and Socket.getOutputStream
• Use the ServerSocket class to create the server side of a TCP/IP socket
Java Programming: Advanced Topics
12
Datagram Sockets
• Datagram sockets send packets that contains information that identifies the network destination in addition to the content of your message
• The class DatagramPacket represents these packets
• The User Datagram Protocol (UDP) used by these sockets makes no attempt to recover from lost or damaged packets
Java Programming: Advanced Topics
13
An Example Datagram Socket Server Program
Java Programming: Advanced Topics
14
An Example Datagram Socket Server Program (Cont.)
Java Programming: Advanced Topics
15
An Example Datagram Socket Server Program (Cont.)
Java Programming: Advanced Topics
16
An Example Datagram Socket Client Program
Java Programming: Advanced Topics
17
An Example Datagram Socket Client Program (Cont.)
Java Programming: Advanced Topics
18
Remote Method Invocation• Remote Invocation Method (RMI) is a
technology that allows programmers to call directly the methods of Java objects that reside on other systems
• Client programs use Object Request Brokers (ORBs) to obtain references to these remote objects
• Marshaling: the technique used to forward the parameters to the remote object
Java Programming: Advanced Topics
19
Remote Method Invocation• Stub classes act as local surrogates for remote
objects to provide access to the remote objects• Skeleton classes marshal and unmarshal
parameters and return values between the stub and the remote object
• On the remote system, a skeleton object receives this byte stream and uses a technique called unmarshaling to deserialize the contents of the parameter list
Java Programming: Advanced Topics
20
Flow of a Remote Method Call
Java Programming: Advanced Topics
21
Developing a Remote Class of Objects
• The Remote interface is a marker interface• To make an interface available remotely via
RMI, it should extend the java.rmi.Remote• All methods in the interface must indicate that
they might throw a java.rmi.RemoteException
Java Programming: Advanced Topics
22
Remote Interface
Java Programming: Advanced Topics
23
Developing an RMI Server Class
Java Programming: Advanced Topics
24
Developing an RMI Server Class (Cont.)
Java Programming: Advanced Topics
25
Developing an RMI Client Class
Java Programming: Advanced Topics
26
Developing an RMI Client Class (Cont.)
Java Programming: Advanced Topics
27
Running the Server and Client Programs
• Compile server and client files using the javac compiler
• Create the stub and skeleton classes by using the rmic command
• Start the RMI registry program by typing:rmiregistry
• Start the server program and supply the codebase for the stub and skeleton files
• Start the client program
Java Programming: Advanced Topics
28
JNDI
• Sun developed the Java Naming and Directory Interface (JNDI)
• JNDI is not itself a naming or directory service, but is a unified interface to multiple naming and directory services that already exist for enterprise development
Java Programming: Advanced Topics
29
JNDI Packages
Java Programming: Advanced Topics
30
JNDI Packages (Cont.)
Java Programming: Advanced Topics
31
Corba, RMI-IIOP, and IDL• Common Object Request Broker Architecture
(CORBA) is a generic architecture for defining and using remote objects
• A consortium of companies, known as the Object Management Group (OMG), developed the CORBA standard
• CORBA ORBs use Internet Inter-ORB Protocol (IIOP)
Java Programming: Advanced Topics
32
Corba, RMI-IIOP, and IDL (Cont.)
• The interfaces of CORBA objects are defined using a language called Interface Definition Language (IDL)
• Java platform SDK provides a tool called idlj that generates the Java bindings for the specified IDL file
Java Programming: Advanced Topics
33
Summary
• The java.net package supports network programming
• The core classes of the java.net package support URLs, TCP/IP sockets, or datagram sockets
• To exchange information with a URL, you must create a URLConnection object
• TCP/IP sockets are connection-oriented, and datagram sockets use packets
Java Programming: Advanced Topics
34
Summary (Cont.)
• Remote Invocation Method (RMI) is a technology that allows programmers to call directly the methods of Java objects that reside on other systems
• The Java Naming and Directory Interface (JNDI) is a unified interface to multiple naming and directory services that already exist for enterprise development
• Common Object Request Broker Architecture (CORBA) is a more generic architecture for defining and using remote objects