Program For Remote Procedure Call (RPC) in JAVA


RPC in JAVA :

Client Program:

cli.java
______________________________________________________________________________

import java.io.*;
import java.net.*;
class cli
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000); 
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); 
OutputStream ostream = sock.getOutputStream(); 
PrintWriter pwrite = new PrintWriter(ostream, true); 
InputStream istream = sock.getInputStream(); 
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));   
System.out.println("Client ready, type and press Enter key");
String receiveMessage, sendMessage,temp; 
while(true) 
{
System.out.println("\nEnter operation to perform(add,sub,mul,div)....");
temp = keyRead.readLine();
sendMessage=temp.toLowerCase(); 
pwrite.println(sendMessage);
System.out.println("Enter first parameter :");
sendMessage = keyRead.readLine(); 
pwrite.println(sendMessage);
System.out.println("Enter second parameter : ");
sendMessage = keyRead.readLine(); 
pwrite.println(sendMessage);
System.out.flush(); 
if((receiveMessage = receiveRead.readLine()) != null) 
System.out.println(receiveMessage); 
}
}
}
______________________________________________________________________________

ser.java
______________________________________________________________________________

import java.io.*;
import java.net.*;
class ser
public static void main(String[] args) throws Exception 
ServerSocket sersock = new ServerSocket(3000); 
System.out.println("Server ready"); 
Socket sock = sersock.accept( ); 
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)); 
OutputStream ostream = sock.getOutputStream(); 
PrintWriter pwrite = new PrintWriter(ostream, true);  
InputStream istream = sock.getInputStream(); 
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));   
String receiveMessage, sendMessage,fun;
int a,b,c;
while(true) 
{
fun = receiveRead.readLine();
if(fun != null) 
System.out.println("Operation : "+fun);
a = Integer.parseInt(receiveRead.readLine());
System.out.println("Parameter 1 : "+a);
b = Integer.parseInt(receiveRead.readLine());
if(fun.compareTo("add")==0)
{
c=a+b;
System.out.println("Addition = "+c);
pwrite.println("Addition = "+c); 
}
if(fun.compareTo("sub")==0)
{
c=a-b;
System.out.println("Substraction = "+c);
pwrite.println("Substraction = "+c); 
}
if(fun.compareTo("mul")==0)
{
c=a*b;
System.out.println("Multiplication = "+c);
pwrite.println("Multiplication = "+c); 
}
if(fun.compareTo("div")==0)
{
c=a/b;
System.out.println("Division = "+c);
pwrite.println("Division = "+c); 
}
System.out.flush();
}
______________________________________________________________________________

14 comments:

  1. we are getting few errors can u please send us the screenshots of the output

    ReplyDelete
    Replies
    1. E:\java>java ser
      Server ready
      Operation : add
      Parameter 1 : 3
      Parameter 2 : 5
      Addition = 8
      Operation : mul
      Parameter 1 : 3
      Parameter 2 : 5
      Multiplication = 15


      E:\java>java cli
      Client ready, type and press Enter key

      Enter operation to perform(add,sub,mul,div)....
      add
      Enter first parameter :
      3
      Enter second parameter :
      5
      Addition = 8

      Enter operation to perform(add,sub,mul,div)....
      mul
      Enter first parameter :
      3
      Enter second parameter :
      5
      Multiplication = 15

      Enter operation to perform(add,sub,mul,div)....

      Delete
    2. you are getting errors maybe because one of your process terminated or you have passed a wrong input...

      Please make sure to start server first then client...on successful startup just pass parameters correctly and you get the right output

      Hope this may help you

      Delete
  2. should I put the 2 different code in 2 different computer? can u please describe how to use it? thanks

    ReplyDelete
    Replies
    1. Yes. Provide just the ip address of the server machine in client socket of client program instead of the local host.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. Bro?? Where u had done the procedural call in this program i mean which is remote procedure here??

    ReplyDelete
  4. thank u so much prog is run successfully

    ReplyDelete
  5. C:\Users\acer\Desktop\DS\PR2>javac cli.java
    cli.java:34: error: class, interface, or enum expected
    ______________________________________________________________________________
    ^
    cli.java:40: error: class, interface, or enum expected
    import java.net.*;
    ^
    2 errors

    I am getting this error, can you help me out to solve

    ReplyDelete
  6. I love your blog because sharing fruitful information about server, and here you can get to get know detail information about Remote Procedure Calls

    ReplyDelete