0% found this document useful (0 votes)
2K views14 pages

Client Server Program Using Remote Procedure Call (RPC)

This document describes a Java program that implements Lamport's logical clock algorithm for distributed system synchronization. It defines classes for representing processes, events, and the mapping of event dependencies. The main method gets input from the user to initialize the number of processes, events per process, and dependency relationships between events. It then calculates the logical clock values for each event based on the dependencies and draws arrows between related events on a graphical output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views14 pages

Client Server Program Using Remote Procedure Call (RPC)

This document describes a Java program that implements Lamport's logical clock algorithm for distributed system synchronization. It defines classes for representing processes, events, and the mapping of event dependencies. The main method gets input from the user to initialize the number of processes, events per process, and dependency relationships between events. It then calculates the logical clock values for each event based on the dependencies and draws arrows between related events on a graphical output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

/* Client Server Program using Remote Procedure Call (RPC) */

// RPC Server Program

import java.io.*;
import java.net.*;

class serrpc
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(3000);
System.out.println("Server ready");
Socket soc = ss.accept( );
DataInputStream dis=new DataInputStream(soc.getInputStream());
PrintWriter pw=new PrintWriter(soc.getOutputStream(),true);
DataInputStream kb=new DataInputStream(System.in);
String receiveMessage, sendMessage,op;
int a,b,c;

while(true)
{
op = dis.readLine();
if(op!= null)
System.out.println("Operation : "+op);
a = Integer.parseInt(dis.readLine());
System.out.println("Parameter 1 : "+a);
b = Integer.parseInt(dis.readLine());
System.out.println("Parameter 2 : "+b);
if(op.compareTo("add")==0)
{
c=a+b;
System.out.println("Addition = "+c);
pw.println("Addition = "+c);
}
if(op.compareTo("sub")==0)
{
c=a-b;
System.out.println("Substraction = "+c);
pw.println("Substraction = "+c);
}
if(op.compareTo("mul")==0)
{
c=a*b;
System.out.println("Multiplication = "+c);
pw.println("Multiplication = "+c);
}

Page | 1
if(op.compareTo("div")==0)
{
c=a/b;
System.out.println("Division = "+c);
pw.println("Division = "+c);
}
System.out.flush();
}
}
}

// RPC Client Program

import java.io.*;
import java.net.*;

class clirpc
{
public static void main(String[] args) throws Exception
{
Socket soc = new Socket("localhost", 3000);
DataInputStream dis=new DataInputStream(soc.getInputStream());
PrintWriter pw=new PrintWriter(soc.getOutputStream(),true);
DataInputStream kb=new DataInputStream(System.in);
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 = kb.readLine();
sendMessage=temp.toLowerCase();
pw.println(sendMessage);
System.out.println("Enter first parameter :");
sendMessage = kb.readLine();
pw.println(sendMessage);
System.out.println("Enter second parameter : ");
sendMessage = kb.readLine();
pw.println(sendMessage);
System.out.flush();
if((receiveMessage = dis.readLine()) != null)
System.out.println(receiveMessage);
}
}
}

Page | 2
/* OUTPUT */

Server side output:

C:\ >java serrpc


Server ready
Operation : add
Parameter 1 : 10
Parameter 2 : 20
Addition = 30
Operation : mul
Parameter 1 : 5
Parameter 2 : 20
Multiplication = 100

Client side output:

C:\ >java clirpc


Client ready, type and press Enter key

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


add
Enter first parameter :
10
Enter second parameter :
20
Addition = 30

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


mul
Enter first parameter :
5
Enter second parameter :
20
Multiplication = 100

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

Page | 3
/* Client Server based Program using Remote Method Invocation (RMI) */

// Interface Program

import java.rmi.*;

public interface MyInterface extends Remote


{
public String countInput(String input)throws RemoteException;
}

//RMI Server Program

import java.rmi.*;
import java.rmi.server.*;

public class RMIServer extends UnicastRemoteObject implements MyInterface


{
public RMIServer()throws RemoteException
{
System.out.println("Remote Server is running Now.!!");
}
public static void main(String arg[])
{
try{
RMIServer p=new RMIServer();
Naming.rebind("rmiInterface",p);
}
catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}

@Override
public String countInput(String input) throws RemoteException
{
System.out.println("Received your input "+ input+" at server!!");
String reply;
reply="You have typed "+ input.length() +" letters!!";
return reply;
}
}

Page | 4
//RMI Client Program

import java.rmi.*;
import java.io.*;
public class RMIClient
{
public static void main(String args[])
{
try
{
DataInputStream d=new DataInputStream(System.in);
MyInterface p=( MyInterface)Naming.lookup("rmiInterface");
System.out.println("Type something...");
String input=d.readLine();
System.out.println(p.countInput(input));
}
catch(Exception e)
{
System.out.println("Exception occurred : "+e.getMessage());
}
}
}

/*OUTPUT*/

Interface output:

C:\rmi>rmiregistry

Server side output:

C:\rmi>java RMIServer
Remote Server is running Now.!!
Received your input welcome to Distributed Systems Lab at server!!

Client side output:

C:\rmi>java RMIClient
Type something...
welcome to Distributed Systems Lab
You have typed 34 letters!!

Page | 5
/* Lamport Clock Synchronization (Logical) */
import java.util.*;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class lamport


{
int e[][]=new int[10][10];
int en[][]=new int[10][10];
int ev[]=new int[10];
int i,p,j,k;
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
int xpoints[] =new int[5];
int ypoints[] =new int[5];
class draw extends JFrame
{
private final int ARR_SIZE = 4;
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2)
{
Graphics2D g = (Graphics2D) g1.create();
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx*dx + dy*dy);
AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
at.concatenate(AffineTransform.getRotateInstance(angle));
g.transform(at);

// Draw horizontal arrow starting in (0, 0)


g.drawLine(0, 0, len, 0);
g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
}

public void paintComponent(Graphics g)


{
for (int x = 15; x < 200; x += 16)
drawArrow(g, x, x, x, 150);
drawArrow(g, 30, 300, 300, 190);
}

public void paint(Graphics g)


{
int h1,h11,h12;
Graphics2D go=(Graphics2D)g;
go.setPaint(Color.black);
for(i=1;i<=p;i++)
{
go.drawLine(50,100*i,450,100*i); }

Page | 6
for(i=1;i<=p;i++)
{
for(j=1;j<=ev[i];j++)
{
k=i*10+j;
go.setPaint(Color.blue);
go.fillOval(50*j,100*i-3,5,5);
go.drawString("e"+i+j+"("+en[i][j]+")",50*j,100*i-5);
h1=hm.get(k);
if(h1!=0)
{
h11=h1/10;
h12=h1%10;
go.setPaint(Color.red);
drawArrow(go,50*h12+2,100*h11,50*j+2,100*i);
}
}
}
}
}
public void calc(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of process:");
p=sc.nextInt();
System.out.println("Enter the no of events per process:");
for(i=1;i<=p;i++)
{
ev[i]=sc.nextInt();
}
System.out.println("Enter the relationship:");
for(i=1;i<=p;i++)
{
System.out.println("For process:"+i);
for(j=1;j<=ev[i];j++)
{
System.out.println("For event:"+(j));
int input=sc.nextInt();
k=i*10+j;
hm.put(k,input);
if(j==1)
en[i][j]=1;
}
}

for(i=1;i<=p;i++)
{
for(j=2;j<=ev[i];j++)
{
k=i*10+j;
if(hm.get(k)==0){ en[i][j]=en[i][j-1]+1; }

Page | 7
else
{
int a=hm.get(k);
int p1=a/10;
int e1=a%10;
if(en[p1][e1]>en[i][j-1])
en[i][j]=en[p1][e1]+1;
else
en[i][j]=en[i][j-1]+1;
}
}
}
for(i=1;i<=p;i++)
{
for(j=1;j<=ev[i];j++)
{ System.out.println(en[i][j]); }
}
JFrame jf=new draw();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.setVisible(true);
}

public static void main(String[] args)


{
lamport lam=new lamport();
lam.calc();
}
}

/* OUTPUT */
C:\>java lamport
Enter the number of process:
2
Enter the no of events per process:
7
5
Enter the relationship:
For process:1
For event:1
0
For event:2
0
For event:3
0
For event:4
0
For event:5
22

Page | 8
For event:6
0
For event:7
24
For process:2
For event:1
0
For event:2
0
For event:3
12
For event:4
0
For event:5
16
1
2
3
4
5
6
7
1
2
3
4
7

Page | 9
/* Election Algorithm (Bully Algorithm) */
import java.io.*;

class EleBully{
static int n;
static int priority[] = new int[100];
static int status[] = new int[100];
static int coordinator;

public static void main(String args[])throws IOException


{
System.out.println("Enter the number of process");
DataInputStream d = new DataInputStream(System.in);
n = Integer.parseInt(d.readLine());
for( int i=0;i<n;i++)
{
System.out.println("For process "+(i+1)+":");
System.out.println("Status:");
status[i]=Integer.parseInt(d.readLine());
System.out.println("Priority");
priority[i] = Integer.parseInt(d.readLine());
}

System.out.println("Which process will initiate election?");


int ele = Integer.parseInt(d.readLine());
election(ele);
System.out.println("Final coordinator is "+coordinator);
}

static void election(int ele)


{
ele = ele-1;
coordinator = ele+1;
for(int i=0;i<n;i++)
{
if(priority[ele]<priority[i])
{
System.out.println("Election message is sent from "+(ele+1)+" to "+(i+1));
if(status[i]==1)
election(i+1);
}
}
}
}

Page | 10
/*OUPUT*/

C:\>java EleBully
Enter the number of process
5
For process 1:
Status:
1
Priority
1
For process 2:
Status:
1
Priority
2
For process 3:
Status:
1
Priority
3
For process 4:
Status:
1
Priority
4
For process 5:
Status:
0
Priority
5
Which process will initiate election?
3
Election message is sent from 3 to 4
Election message is sent from 4 to 5
Election message is sent from 3 to 5
Final coordinator is 4

Page | 11
/* Multi-threaded Client/Server Process */

//Sever Program

import java.io.*;
import java.net.*;

class MS
{
public static void main(String args[])
{
try{
ServerSocket ss=new ServerSocket (3339);
int client=1;
while(true){
Socket soc=ss.accept();
ThreadHandler th=new ThreadHandler(soc,client);
th.start();
client++;
}
}catch(Exception e){e.printStackTrace();}
}
}

class ThreadHandler extends Thread


{
Socket soc;
int count;
ThreadHandler(Socket so,int c){soc=so;count=c;}
public void run()
{
try{
DataInputStream dis=new DataInputStream(soc.getInputStream());
PrintWriter pw=new PrintWriter(soc.getOutputStream(),true);
DataInputStream kb=new DataInputStream(System.in);
pw.println("Server is Running..");

while(true)
{
System.out.println("C-"+count+":"+dis.readLine());
System.out.println("Message to Client: ");
pw.println(kb.readLine());
}
}catch(Exception e){e.printStackTrace();}
}
}

Page | 12
//Client Program

import java.io.*;
import java.net.*;

class C
{
public static void main(String args[])
{
try{
Socket soc=new Socket ("localhost",3339);
DataInputStream dis=new DataInputStream(soc.getInputStream());
PrintWriter pw=new PrintWriter(soc.getOutputStream(),true);
DataInputStream kb=new DataInputStream(System.in);
System.out.println(dis.readLine());
String str;
while(true)
{
System.out.println("Message to server");
pw.println(kb.readLine());
str=dis.readLine();
if(str.equals("End")) break;
System.out.println("From Server: "+str);
}
}catch(Exception e){e.printStackTrace();}
}
}

/*OUTPUT*/

Server side output:

C:\>java MS
C-1:Hai Iam Amell
Message to Client:
Hai Amell
C-2:Hai Iam Robert
Message to Client:
Hai Robert

Client side output:

C:\>java C
Server is Running..
Message to server
Hai Iam Amell
From Server: Hai Amell
Message to server

Page | 13
C:\>java C
Server is Running..
Message to server
Hai Iam Robert
From Server: Hai Robert
Message to server

Page | 14

You might also like