AIM:
To write a java
program for network operating systems
ALGORITHM:
Step 1:
Start the program
Interface program
Step 2:
Create an interface ReceiveMessageInterface which
extends Remote class
Step 3:
Declare method receiveMessage inside interface
Server Program
Step 4:
Create class RmiServer which extends UnicastRemoteObject
which again extends the ReceiveMessageInterface
Step 5:
Define the method receiveMessage which get parameter x
Step 6:
Declare a Random object which is used to get random
integers
Step 7:
Using the value of x get the Random number one by one
and store it in val[i]
Step 8:
Define a constructor RmiServer in that get the local
address of the Server by using InetAddress.getLocalHost()
Step 9:
Declare a port and register it in the registry object
Step 10:
Using Rebind method we register the port and a string “rmiserver”
into the rmiregistry table
Step 11:
Create a object using RmiServer so that the Constructor
will be initialized automatically
Client program
Step 12:
Create class RmiClient
Step 13:
Declare an object rmiServer for ReceiveMessageInterface
Step 14:
Get the server’s local address and the port in command
line arguments
Step 15:
Locate the registry object using the server port
Step 16:
Using registry object call the method lookup of the
server string “rmiserver” by casting the interface which is already defined
Step 17:
Now using the rmiServer object call the method
receiveMessage and receive all random number one by one from the server
Step 18:
End of
programs
PROGRAM:-
ReceiveMessageInterface:
import java.rmi.*;
public interface ReceiveMessageInterface extends
Remote
{
public int
[] receiveMessage(int x) throws RemoteException;
}
RmiClient.java:
import java.io.*;
import java.rmi.*;
import
java.rmi.registry.*;
import java.net.*;
public class
RmiClient
{
static public void main(String
args[])throws IOException
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
int no;
no=Integer.parseInt(args[2]);
int rval[]=new int[no];
System.out.println("sending
"+no+" to "+serverAddress+":"+serverPort);
try{
registry=LocateRegistry.getRegistry(serverAddress,(new
Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
rval=rmiServer.receiveMessage(no);
for(int i=0;i<no;i++)
System.out.println(rval[i]);
}
catch(RemoteException e)
{
e.printStackTrace();
}
catch(NotBoundException e)
{
e.printStackTrace();
}
}
}
RmiServer.java:
import java.util.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends
java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry
registry;
public
int[] receiveMessage(int x) throws RemoteException
{
System.out.println(x);
Random
r=new Random();
int
val[] = new int[x];
for(int
i=0;i<x;i++)
{
val[i]=r.nextInt(25);
}
return
val;
}
public
RmiServer() throws RemoteException
{
try
{
thisAddress=
(InetAddress.getLocalHost()).toString();
}
catch(Exception e)
{
throw new RemoteException("can't get inet address.");
}
thisPort=3232;
System.out.println("this
address="+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 s=new RmiServer();
}
catch
(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
Output:
U:\OS_Lab\EX5\RMI>javac RmiServer.java
U:\OS_Lab\EX5\RMI>java RmiServer
this address=rdl30/180.100.102.30,port=3232
U:\OS_Lab\EX5\RMI>javac RmiClient.java
U:\OS_Lab\EX5\RMI>java RmiClient 180.100.102.30
3232 4
sending 4 to 180.100.102.30:3232
6
7
10
24
No comments:
Post a Comment