C:\jtest\RMI>java lineno < Hello.java 1 //Hello.java 2 //package example.hello; 3 import java.rmi.Remote; 4 import java.rmi.RemoteException; 5 public interface Hello extends Remote { 6 String sayHello() throws RemoteException; 7 }// interface Hello C:\jtest\RMI>java lineno < Server.java 1 //Server.java 2 //package example.hello; 3 import java.rmi.registry.Registry; 4 import java.rmi.registry.LocateRegistry; 5 import java.rmi.RemoteException; 6 import java.rmi.server.UnicastRemoteObject; 7 public class Server implements Hello { 8 public Server() {} 9 public String sayHello() { 10 return "Hello, world!"; 11 } 12 public static void main(String args[]) { 13 try { 14 Server obj = new Server(); 15 Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0); 16 17 // Bind the remote object's stub in the registry 18 Registry registry = LocateRegistry.getRegistry(); 19 registry.bind("Hello", stub); 20 21 System.err.println("Server ready"); 22 } catch (Exception e) { 23 System.err.println("Server exception: " + e.toString()); 24 e.printStackTrace(); 25 } 26 } // main( 27 }//class Server C:\jtest\RMI>java lineno < Client.java 1 //Client.java 2 //package example.hello; 3 import java.rmi.registry.LocateRegistry; 4 import java.rmi.registry.Registry; 5 public class Client { 6 private Client() {} 7 public static void main(String[] args) { 8 String host = (args.length < 1) ? null : args[0]; 9 try { 10 Registry registry = LocateRegistry.getRegistry(host); 11 Hello stub = (Hello) registry.lookup("Hello"); 12 String response = stub.sayHello(); 13 System.out.println("response: " + response); 14 } catch (Exception e) { 15 System.err.println("Client exception: " + e.toString()); 16 e.printStackTrace(); 17 } 18 }//main( 19 }// class Client C:\jtest\RMI>javac *java C:\jtest\RMI>start rmiregistry C:\jtest\RMI>javaw Server C:\jtest\RMI>java Client response: Hello, world! C:\jtest\RMI>java Client 192.168.1.2 response: Hello, world! C:\jtest\RMI>java Client 192.168.1.1 Client exception: java.rmi.ConnectException: Connection refused to host: 192.168 .1.1; nested exception is: java.net.ConnectException: Connection timed out: connect