Professional Documents
Culture Documents
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();
}
}
}
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 */
Page | 3
/* Client Server based Program using Remote Method Invocation (RMI) */
// Interface Program
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
@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
C:\rmi>java RMIServer
Remote Server is running Now.!!
Received your input welcome to Distributed Systems Lab at server!!
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.*;
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);
}
/* 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;
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();}
}
}
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*/
C:\>java MS
C-1:Hai Iam Amell
Message to Client:
Hai Amell
C-2:Hai Iam Robert
Message to Client:
Hai Robert
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