Interface implementation:
/* * RMIInterface.java * * Created on 29 agosto 2001, 16.21 */ package RMIBin; import java.rmi.*; /** * * @author Andrea Piovesan * @version */ public interface RMIInterface extends Remote { public void sendText(String str) throws RemoteException; }
Server implementation: /* * RMIServer.java * * Created on 29 agosto 2001, 16.17 */ package RMIBin; import java.net.*; import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; /** * Servre RMI per la ricezione di una stringa da un client. * @author Andrea Piovesan * @version 1.0 */ public class RMIServer extends UnicastRemoteObject implements RMIInterface { /** Creates new RMIServer */ public RMIServer() throws RemoteException { } /** Metodo chiamato dal client per mandare una stringa al server * @param Stringa che il server riceve. */ public void sendText(String str) throws RemoteException { System.out.println(str); } public static void main(String[] args) { try { System.setSecurityManager(new RMISecurityManager()); RMIServer server = new RMIServer(); Naming.bind("//Masterpiove:1010/RMIServer", server); } catch (Exception e) { System.out.println("Errore #1: " + e); e.printStackTrace(); } } }
Client implementation:
/* * Class.java * * Created on 29 agosto 2001, 16.47 */ package RMIBin; import java.rmi.*; import java.rmi.registry.*; /** * * @author Andrea Piovesan * @version */ public class RMIClient extends java.lang.Object { /** Creates new Class */ public RMIClient() { } public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { RMIInterface server = (RMIInterface)Naming.lookup("//Masterpiove:1010/RMIServer"); server.sendText("Ciao, sono il client!"); } catch (Exception e) { System.out.println("Errore:" + e); } } }