Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Tuesday, 27 September 2011

RPC and RMI operation in java


Client
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrpc
{
            public static void main(String args[])
            {
            try
            {
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        Socket clsct=new Socket("127.0.0.1",139);
                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());

                        System.out.println("Enter String");
                        String str=in.readLine();
                        dout.writeBytes(str+'\n');
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrpc
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                        while(true)
                        {
                                    Socket obj1=obj.accept();
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str=din.readLine();
                                    Process p=Runtime.getRuntime().exec(str);
                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
OUTPUT
Server
Y:\networks\remote>java Serverrpc
Client
Y:\networks\remote>java Clientrpc
Enter String
calc












Rmi
Client
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.*;
import java.net.*;

class clirmi
  {
 
     public static void main(String args[])
{
         try
           {
              calc o= new calcim();
            Naming.lookup("rmi://165.165.1.1/calculatoroperation");
               System.out.println("Addition:"+o.add(4,5));
               System.out.println("Subtraction:"+o.sub(7,5));
            System.out.println("Multiplication:"+o.mul(4,5));
             System.out.println("Division:"+o.div(10,5));
              }
   catch (Exception e)
         {
            System.out.println(e);
        }
       
    }

   }
Server
Interface
public interface calc extends java.rmi.Remote
  {
    public long  add(long a,long b) throws java.rmi.RemoteException;
    public long  sub(long a,long b) throws java.rmi.RemoteException;
    public long  mul(long a,long b) throws java.rmi.RemoteException;
    public long  div(long a,long b) throws java.rmi.RemoteException;
  }
Implementation of interface
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
class calcim extends java.rmi.server.UnicastRemoteObject implements calc
  {
     public calcim() throws  java.rmi.RemoteException
        {
           super();
         }
     public  long add( long a,long b) throws  java.rmi.RemoteException
       {
           return (a+b);
        }
       public long  sub( long a,long b) throws  java.rmi.RemoteException
       {
           return (a-b);
        }
 public  long mul( long a,long b) throws  java.rmi.RemoteException
       {
           return (a*b);
        }
 public long div( long a,long b) throws  java.rmi.RemoteException
       {
           return (a/b);
        }

 }
Main file
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.*;
import java.net.*;

public class serrmi
  {
      serrmi()
        {
           try{
                  calc ob=new calcim();
                 Naming.rebind("rmi://165.165.1.13/calculatoroperation",ob);
               }
           catch(Exception e)
               {
                }
        }
       public static void main(String args[])
          {
            new serrmi();
          }
   } 
Output
D:\Java\jdk1.5.0_05\bin>javac serrmi.java

D:\Java\jdk1.5.0_05\bin>javac calc.java

D:\Java\jdk1.5.0_05\bin>javac calcim.java

D:\Java\jdk1.5.0_05\bin>rmic -verbose serrmi
[loaded .\serrmi.class in 15 ms]
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/lang/Object.class) in 15
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/rmi/Remote.class) in 0 ms
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/lang/Exception.class) in
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/lang/Throwable.class) in
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/io/Serializable.class) in
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/rmi/RemoteException.class
ms]
[loaded D:\Java\jdk1.5.0_05\jre\lib\rt.jar(java/io/IOException.class) in
error: Class serrmi does not implement an interface that extends java.rmi
; only remote objects need stubs and skeletons.
1 error
[done in 266 ms]
D:\Java\jdk1.5.0_05\bin>start rmiregistry

D:\Java\jdk1.5.0_05\bin>java serrmi
Client
D:\Java\jdk1.5.0_05\bin>javac clirmi.java

D:\Java\jdk1.5.0_05\bin>java clirmi
Addition:9
Subtraction:2
Multiplication:20
Division:2

No comments:

Post a Comment

Followers