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();
}
}
}
______________________________________________________________________________
we are getting few errors can u please send us the screenshots of the output
ReplyDeleteE:\java>java ser
DeleteServer 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)....
you are getting errors maybe because one of your process terminated or you have passed a wrong input...
DeletePlease 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
thnx
ReplyDeleteshould I put the 2 different code in 2 different computer? can u please describe how to use it? thanks
ReplyDeleteYes. Provide just the ip address of the server machine in client socket of client program instead of the local host.
DeleteThis comment has been removed by the author.
Deletethnx
ReplyDeleteBro?? Where u had done the procedural call in this program i mean which is remote procedure here??
ReplyDeletehow to add exit() command in it
ReplyDeletethank you sir !
ReplyDeletethank u so much prog is run successfully
ReplyDeleteC:\Users\acer\Desktop\DS\PR2>javac cli.java
ReplyDeletecli.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
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