0% found this document useful (0 votes)
129 views3 pages

LAb Assignment 3 FEB-2018

This document provides code for implementing remote method invocation (RMI) between a server and client in Java. [1] It defines the RMI interface with a receiveMessage method. [2] The RMI server code implements this interface and registers the service with the RMI registry. [3] The RMI client code looks up the registered service and calls the receiveMessage method, passing a string message to the server. The client output indicates whether the call was successful.

Uploaded by

David Dasie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views3 pages

LAb Assignment 3 FEB-2018

This document provides code for implementing remote method invocation (RMI) between a server and client in Java. [1] It defines the RMI interface with a receiveMessage method. [2] The RMI server code implements this interface and registers the service with the RMI registry. [3] The RMI client code looks up the registered service and calls the receiveMessage method, passing a string message to the server. The client output indicates whether the call was successful.

Uploaded by

David Dasie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DISTRIBUTED SYSTEM-FEB-18

Assignment 3 – Lab 3

Remote Method Invocation

i) Compile the following codes separately and run the RMI server
and then RMI client.
ii) Look for the output. change the coding for different port number
iii) Does you client responds? If not analyze the program.

1) The following code defines the RMI interface

import java.rmi.*;

public interface ReceiveMessageInterface extends Remote{


void receiveMessage(String x) throws RemoteException;
}

2) Following is the code for the code of RMI Server

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends


java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{
String address;
Registry registry;

public void receiveMessage(String x) throws RemoteException{


System.out.println(x);
}

public RmiServer() throws RemoteException{


try{
address = (InetAddress.getLocalHost()).toString();
}
DISTRIBUTED SYSTEM-FEB-18

catch(Exception e){
System.out.println("can't get inet address.");
}
int port=3232;
System.out.println("this address=" + address + ",port=" + port);
try{
registry = LocateRegistry.createRegistry(port);
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
System.out.println("remote exception"+ e);
}
}
static public void main(String args[]){
try{
RmiServer server = new RmiServer();
}
catch (Exception e){
e.printStackTrace();
System.exit(1);
}
}
}

3) Following is the code for the code of RMI client

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient{


static public void main(String args[]){
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println ("sending " + text + " to " +serverAddress + ":" + serverPort);
try{
registry=LocateRegistry.getRegistry
(serverAddress,(new Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
DISTRIBUTED SYSTEM-FEB-18

// call the remote method


rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
System.err.println(e);
}
}
}

You might also like