The code for the client program RMIExampleClient
described in the book is shown below. Note that it belongs to the
package javatech.rmi18.client and so must
reside in the client subdirectory as shown by this path:
javatech
|
|__ rmi18
|
|__ client
RMIExampleClient.java
|
// RMIExampleClient.java
package javatech.rmi18.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import javatech.rmi18.server.RMIExampleInterface;
/** A client that makes remote calls to the RMI server developed
in
* Chapter 18. **/
public class RMIExampleClient
{
/** Constructor **/
public RMIExampleClient () {
// Create and install a security
manager.
if (System.getSecurityManager()
== null) {
System.setSecurityManager(new
RMISecurityManager());
}
// Get a reference to the remote
server named rmi-example-server.
RMIExampleInterface rmi_example_server
= null;
try {
// Find the server.
String server_name =
"//localhost:3001/rmi-example-server";
System.err.println (
"RMIExampleClient:
looking up server " + server_name
);
rmi_example_server =
(RMIExampleInterface)
Naming.lookup (server_name);
System.err.println ("RMIExampleClient:
found it!");
} // try
catch (MalformedURLException mue)
{
System.err.println (mue);
}
catch (NotBoundException nbe) {
System.err.println (nbe);
}
catch (RemoteException re) {
System.err.println (re);
}
if (rmi_example_server == null)
{
System.err.println ("\nEXITING
BECAUSE OF FAILURE");
System.exit (1);
}
// Test method1.
System.err.println (
"Calling remote method1
which should echo the string" +
" 'hello from client'"
);
try {
rmi_example_server.method1
("hello from client");
}
catch (RemoteException re) {
System.err.println (re);
}
// Test the add method.
try {
int sum = rmi_example_server.add
(18, 34);
System.err.println (
"According
to the remote add(), the sum of 18 and 34 is " + sum
);
}
catch (RemoteException re) {
System.err.println (re);
}
} // ctor
public static void main (String[] args) {
new RMIExampleClient ();
} // main
} // class RMIExampleClient
|
The following version is the same as above except for the server
name.
RMIExampleClient.java.remote
|
// RMIExampleClient.java
package javatech.rmi18.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import javatech.rmi18.server.RMIExampleInterface;
/** A client that makes remote calls to the RMI server developed
in
* Chapter 18. **/
public class RMIExampleClient
{
/** Constructor **/
public RMIExampleClient () {
// Create and install a security
manager.
if (System.getSecurityManager()
== null) {
System.setSecurityManager(new
RMISecurityManager());
}
// Get a reference to the remote
server named rmi-example-server.
RMIExampleInterface rmi_example_server
= null;
try {
// Find the server.
String server_name =
"//angel:3001/rmi-example-server";
System.err.println (
"RMIExampleClient:
looking up server " + server_name
);
rmi_example_server =
(RMIExampleInterface)
Naming.lookup (server_name);
System.err.println ("RMIExampleClient:
found it!");
} // try
catch (MalformedURLException mue)
{
System.err.println (mue);
}
catch (NotBoundException nbe) {
System.err.println (nbe);
}
catch (RemoteException re) {
System.err.println (re);
}
if (rmi_example_server == null)
{
System.err.println ("\nEXITING
BECAUSE OF FAILURE");
System.exit (1);
}
// Test method1.
System.err.println (
"Calling remote method1
which should echo the string" +
" 'hello from client'"
);
try {
rmi_example_server.method1
("hello from client");
}
catch (RemoteException re) {
System.err.println (re);
}
// Test the add method.
try {
int sum = rmi_example_server.add
(18, 34);
System.err.println (
"According
to the remote add(), the sum of 18 and 34 is " + sum
);
}
catch (RemoteException re) {
System.err.println (re);
}
} // ctor
public static void main (String[] args) {
new RMIExampleClient ();
} // main
} // class RMIExampleClient
|
See the RMI example support files page
for the policy file plus the build and run command files for the
client.
Most recent update: Oct.12.05
|