RPC Application
PROBLEM STATEMENT:
Write a RPC application to add two numbers.
SOURCE CODE
Server.java
Client.java
OUTPUT
Client.java
C:\Documents and Settings\Nids\Desktop\RPC>java Client
add 33 66
Result: 99
Server.java
C:\Documents and Settings\Nids\Desktop\RPC>java Server
3 add
add 33 66
PROBLEM STATEMENT:
Write a RPC application to add two numbers.
SOURCE CODE
Server.java
import java.util.*;
import java.net.*;
class Server
{
DatagramSocket ds;
String methodName;
int param1,param2;
Server()
{
try
{
ds = new DatagramSocket(1200);
byte b[] = new byte[4096];
while (true)
{
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
if (str.equals("q"))
{
System.exit(1);
}
else
{
StringTokenizer st = new StringTokenizer(str," "); //<-- space is the delimiter, e.g. add 6 7
int i = 0;
while (st.hasMoreTokens())
{
String token = st.nextToken();
if (i==0)
{
methodName = token;
System.out.println(methodName.length()+" "+methodName);
}
else if (i==1)
{
param1 = Integer.parseInt(token);
}
else if (i==2)
{
param2 = Integer.parseInt(token);
}
i++;
}
}
System.out.println(str);
if (methodName.equals("add"))
{
InetAddress ia = InetAddress.getLocalHost();
String result = "" + add(param1,param2);
byte b1[] = result.getBytes();
DatagramSocket ds1 = new DatagramSocket();
DatagramPacket dp1 = new DatagramPacket(b1,b1.length,ia,1201);
ds1.send(dp1);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public int add(int a,int b)
{
return a+b;
}
public static void main(String[] args)
{
new Server();
}
}
import java.net.*;
class Server
{
DatagramSocket ds;
String methodName;
int param1,param2;
Server()
{
try
{
ds = new DatagramSocket(1200);
byte b[] = new byte[4096];
while (true)
{
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String str = new String(dp.getData(),0,dp.getLength());
if (str.equals("q"))
{
System.exit(1);
}
else
{
StringTokenizer st = new StringTokenizer(str," "); //<-- space is the delimiter, e.g. add 6 7
int i = 0;
while (st.hasMoreTokens())
{
String token = st.nextToken();
if (i==0)
{
methodName = token;
System.out.println(methodName.length()+" "+methodName);
}
else if (i==1)
{
param1 = Integer.parseInt(token);
}
else if (i==2)
{
param2 = Integer.parseInt(token);
}
i++;
}
}
System.out.println(str);
if (methodName.equals("add"))
{
InetAddress ia = InetAddress.getLocalHost();
String result = "" + add(param1,param2);
byte b1[] = result.getBytes();
DatagramSocket ds1 = new DatagramSocket();
DatagramPacket dp1 = new DatagramPacket(b1,b1.length,ia,1201);
ds1.send(dp1);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public int add(int a,int b)
{
return a+b;
}
public static void main(String[] args)
{
new Server();
}
}
Client.java
import java.io.*;
import java.net.*;
class Client
{
Client()
{
try
{
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket(1201);
while (true)
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = br.readLine();
byte b[] = str.getBytes();
DatagramPacket dp = new DatagramPacket(b,b.length,ia,1200);
ds.send(dp);
dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
System.out.println("Result:"+s);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
import java.net.*;
class Client
{
Client()
{
try
{
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket(1201);
while (true)
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = br.readLine();
byte b[] = str.getBytes();
DatagramPacket dp = new DatagramPacket(b,b.length,ia,1200);
ds.send(dp);
dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
System.out.println("Result:"+s);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Client();
}
}
OUTPUT
Client.java
C:\Documents and Settings\Nids\Desktop\RPC>java Client
add 33 66
Result: 99
Server.java
C:\Documents and Settings\Nids\Desktop\RPC>java Server
3 add
add 33 66
0 comments:
Confused? Feel free to ask
Post a Comment