// GetMessageInterface.java
import java.rmi.*;
public interface GetMessageInterface
extends Remote{
void GetMessage
(String str
) throws RemoteException;
}
// RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer
extends java.
rmi.
server.
UnicastRemoteObject implements GetMessageInterface
{
int thisPort
;
String thisAddress
;
Registry registry
;
public void GetMessage
(String str
) throws RemoteException{
System.
out.
println(str
);
}
public RmiServer
() throws RemoteException{
try{
thisAddress
= (InetAddress.
getLocalHost()).
toString();
}
catch(Exception e
){
throw new RemoteException("Unable to get inet address.");
}
thisPort
= 1983;
System.
out.
println("Server started on =" + thisAddress
+ ", port=" + thisPort
);
try{
registry
= LocateRegistry.
createRegistry( thisPort
);
registry.
rebind("rmiServer",
this);
}
catch(RemoteException e
){
throw e
;
}
}
static public void main
(String args
[]){
try{
RmiServer server
=new RmiServer
();
}
catch (Exception e
) {
e.
printStackTrace();
System.
exit(1);
}
}
}
// RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main
(String args
[])
{
GetMessageInterface rmiServer
;
Registry registry
;
String serverAddress
= "127.0.0.1";
int serverPort
= 1983;
String text
= "Hello World";
System.
out.
println("sending "+text
+" to "+serverAddress
+":"+serverPort
);
try{
registry
= LocateRegistry.
getRegistry( serverAddress, serverPort
);
rmiServer
= (GetMessageInterface
)(registry.
lookup("rmiServer"));
rmiServer.
GetMessage(text
);
}
catch(RemoteException e
){
e.
printStackTrace();
}
catch(NotBoundException e
){
e.
printStackTrace();
}
}
}