Showing posts with label Distributed Computing. Show all posts
Showing posts with label Distributed Computing. Show all posts

Sunday, 5 February 2017



RPC Program for Simple Calculator





Submitted By: Shreeram

Prof.Mahendra Kanojia

RMI Program to Display Addition of Two Numbers




Submitted By - Mr. Shreeram.


Prof.Mahendra Kanojia

Remote Method Invocation







Prof.Mahendra Kanojia

Thursday, 13 December 2012


RPC Application

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();
    }
}

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();
    }
}

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
Prof.Mahendra Kanojia

Building Client-Server Communication for Exchanging String Messages

PROBLEM STATEMENT:

Write a program to demonstrate the exchange of messages between a client and a server.

SOURCE CODE

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

public class Client
{
   
 static int port;
  static int defaultport=9000;
  static InetAddress host;

Client(int port,InetAddress host)
    {
        this.port=port;
        this.host=host;
    }

public static void main(String args[])
        {
            try{
             int port = defaultport;
             host=InetAddress.getLocalHost();
             Client cl = new Client(port,host);
             cl.start();
            }
           
            catch(Exception e)
                {
                    e.printStackTrace();
                }
        }
 


  public void start()
    {
        try
        {
            DatagramSocket ds=new DatagramSocket();
            DatagramPacket snddp=null;
            byte buf[] =new byte[256];
            String req,res;
            BufferedReader br;
    br=new BufferedReader(new InputStreamReader(System.in));
                   
            do{
            String str = br.readLine();
    snddp = new DatagramPacket(str.getBytes(),str.length(),host,port);
            ds.send(snddp);
   
            //ds = new DatagramSocket(port);
            DatagramPacket dp = new DatagramPacket(buf,buf.length);       
            ds.receive(dp);
            res = new String(dp.getData(),0,dp.getLength());
           
                if(res.equals("ono"))
                {
                    System.out.println("Connection closed :( ");
                    System.exit(0);
                }   
                System.out.println(res);
        }while(!(res.equals("ono")));   
           
        }
    catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}   

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

public class Server
{
   
 static  int port;
  static int defaultport=9000;

Server(int port)
    {
        this.port=port;
    }


public static void main(String args[])
        {
            int port = defaultport;
            Server ser = new Server(port);
            ser.start();
        }


  public void start()
    {
        try
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte buf[] =new byte[256];
            String req,res;
            InetAddress lclhost;
            int sendport,inport;
            BufferedReader br;

            ds = new DatagramSocket(port);
            dp = new DatagramPacket(buf,buf.length);

            do{
                   
            ds.receive(dp);
            req = new String(dp.getData(),0,dp.getLength());
            lclhost = dp.getAddress();
            inport = dp.getPort();


            if(!(req.equals("ono")))
            {
                System.out.println(req);
                br=new BufferedReader(new InputStreamReader(System.in));
                res=br.readLine();
DatagramPacket dp1 = new DatagramPacket(res.getBytes(),res.length(),lclhost,inport);
ds.send(dp1);
            }

else
System.out.println("CONNECTION CLOSED :( ");
}while(!(req.equals("ono")));   
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

}
}

OUTPUT

Server.java
F:\MSC\DCpracs\clent_server_prac10>java Server
hello
hi
how r u?
I m doing good
Bye...cya...
Ono

Client.java
F:\MSC\DCpracs\clent_server_prac10>java Client
hello
hi
how r u?
I m doing good
Bye...cya...
Connection closed :(
Prof.Mahendra Kanojia

Remote Objects for Database Access

PROBLEM STATEMENT

Write an RMI program in Java to access a database and also must have appropriate methods for inserting,deleting a retrieval of records from the database.

SOURCE CODE

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

interface face1 extends Remote
{
public String[] retrieve() throws RemoteException;
public void addName(int eno,String  name) throws RemoteException;
public void del(String eno) throws RemoteException;
}

faceserver.java
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
import sun.jdbc.odbc.*;
public  class faceserver extends UnicastRemoteObject implements face1
{
    public int count;
    public String roll[];
    public String name[];

    public faceserver() throws RemoteException
    {
    }

    public String[] retrieve() throws RemoteException
    {
        try
        {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection c1 = DriverManager.getConnection("jdbc:odbc:mast");
        Statement s1 = c1.createStatement();
        ResultSet rs = s1.executeQuery("select * from mast");
        int count=0;       
            int i;
        name = new String[20];   

            while(rs.next())
            {
                name[count] = rs.getString(1) + "      " +  rs.getString(2);
                count++;       
            }
       
    roll = new String[count];
            for(i=0;i<count;i++)
            {
                roll[i] = name[i];
            }
        }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        return roll; 
    }


public void addName(int eno,String name) throws RemoteException
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:mast");
       
            String s1 = "insert into mast values(?,?)";
            PreparedStatement stmt = con.prepareStatement(s1);
            stmt.setInt(1,eno);
            stmt.setString(2,name);

             stmt.executeUpdate();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
public void del(String no) throws RemoteException
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:mast");
            String s1 = "delete from mast where EmpNo =no ";
        Statement s2 = con.createStatement();
             s2.execute("delete from mast where EmpNo =no ");
   
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }               
    public static void main(String ae[]) throws Exception
    {
    faceserver f1 = new faceserver();
    System.out.println("Trying to register the object");
    Naming.bind("NAME1",f1);//f1 is given an identity NAME
    System.out.println("Object registered");
    }
}

faceclient.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;

public abstract class faceclient
{
public static void main(String ad[]) throws Exception
{
BufferedReader br;
String name,en;
int eno,choice;
face1 f2 =(face1)Naming.lookup("NAME1");

System.out.println("Press 1 to view records");
System.out.println("Press 2 to add records");
           System.out.println("Press 3 to delete records");
System.out.println("Enter Your Choice:");
      br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
choice=Integer.parseInt(en);

switch(choice)
{
case 1:
{
        String a[] = f2.retrieve();
                    System.out.println("Eno     Employee names:");
            for(int i=0;i<a.length;i++)
                          {
                    System.out.println(a[i]);
}
        break;       
}

Case 2:
{
        System.out.println("Enter Eno:");
        br=new BufferedReader(new InputStreamReader(System.in));
  en=br.readLine();
  eno=Integer.parseInt(en);
                 System.out.println("Enter Employee name:");
        br=new BufferedReader(new InputStreamReader(System.in));0
                        name=br.readLine();
        f2.addName(eno,name);
   break;
            }
case 3:
{
                System.out.println("Enter Eno to delete:");
        br=new BufferedReader(new InputStreamReader(System.in));
   en=br.readLine();

         f2.del(en);
}
default:
{
System.exit(0);
}
                }
}
}

OUTPUT

Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
2
Enter Eno:
113
Enter Employee name:
Vinita
Press any key to continue . . .

Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
1
Eno      Employee names:
545      Satish
876      Sameer
898      Prashant
546      Sai
989      Farhan
656      Amit
675      Crima
101      Nidhi
113      Vinita
Press any key to continue . . .
Prof.Mahendra Kanojia

Building simple Client-Server system using RMI

PROBLEM STATEMENT:

Write a program to implement an RMI application.

DESCRIPTION

DEFINTION OF RMI

Allows a Java method to obtain a reference to a remote object and
invoke methods of the remote object nearly as easily as if the
remote object existed locally

RMI APPLICATION

RMI STUBS AND SKELETONS

RMI uses stub and skeleton objects to provide the connection
between the client and the remote object
     A stub is a proxy for a remote object which is responsible for forwarding method invocations from the client to the server
where the actual remote object implementation resides
 A client's reference to a remote object, therefore, is actually a reference to a local stub. The client has a local copy of the stub object.
 A skeleton is a server-side object which contains a method that dispatches calls to the actual remote object implementation
A remote object has an associated local skeleton object to
dispatch remote calls to it

SOURCE CODE

RMIPracIF.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public interface RMIPracIF extends Remote
    {
        public String welcome(String str)throws RemoteException;
       
    }

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

public class RMIIFImpl extends UnicastRemoteObject implements RMIPracIF
{
   
       public RMIIFImpl()throws RemoteException{}

    public String welcome(String str)
    {
        String res="Hi," + str + " have a great time in the Corporate World";
        return res;
    }
}
   

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

public class RMIServer extends RMIIFImpl
    {
       
        RMIServer()throws Exception{}
        public static void main(String a[])throws Exception
    {
        RMIIFImpl ri=new RMIIFImpl();
        Naming.rebind("StudName",ri);
    System.out.println("Server initiated....");
    }
}   

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

public class RMIClient
{private static final String HOST_NAME="localhost";
 public static void main(String arg[]) throws Exception
    {
       
        try
        {
            RMIPracIF r = (RMIPracIF)Naming.lookup("//"+HOST_NAME+"/StudName");
            String s1 = r.welcome("Priya");
            System.out.println(s1);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

Step 1
Compile all 4 progs using command line tool javac

Step 2
Create stub and skeleton for the Implementation class using command line tool rmic -vcompat <Implclassfile>

Step 3
Start the rmiregistry on a different command window

Step 4
Run the Server and client program on two different command windows.


OUTPUT

C:\priya\DCpracs\SimpleRMI_prac8>java RMIServer
Server initiated....

C:\priya\DCpracs\SimpleRMI_prac8>java RMIClient
Hi,Priya have a great time in the Corporate World
Prof.Mahendra Kanojia

Two-Phase Commit Protocol

PROBLEM STATEMENT

Write a program to implement a two-phase commit protocol. Messages are passed between ‘n’ nodes (or terminals) and actions are committed or aborted, with respect to the response of other nodes. The final action is the n communicated to all other nodes.

DESCRIPTION

Two-Phase Commit Protocol follows following steps;

Step1:
The Coordinator sends a VOTE_REQUEST message to all participants.

Step2:
When a participant receives a VOTE_REQUEST message,it returns either a VOTE_COMMIT message to the coordinator telling the coordinator that is prepared to commit or a VOTE_ABORT message.

Step3:
The coordinator collects all votes from the participants. If all participants had voted to commit the transaction then it sends GLOBAL_COMMIT message to all participants. Even if one participant had voted to abort the transaction then it send GLOBAL_ABORT message.

Step4:
Each participant that voted for a commit waits for the final reaction by the coordinator. If a participant receives a GLOBAL_COMMIT message it locally commits the transaction else it abort the local transaction.

      
SOURCE CODE:

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


public class TPCServer
{

    public static void main(String a[])throws Exception
        {
            BufferedReader br;
            InetAddress lclhost;
            lclhost=InetAddress.getLocalHost();
            Server ser=new Server(lclhost);
           
            System.out.println("Server in sending mode.....");
           

            // Sending data to client 1
            ser.setSendPort(9000);   //recport=8000
            ser.setRecPort(8001);   //sendport=9001

            System.out.println("Send request data to client1..");
        br=new BufferedReader(new InputStreamReader(System.in));
        String s=br.readLine();
            System.out.println("Data is "+s);
            ser.sendData();
            System.out.println("Waiting for response from client1....");
            ser.recData();
   
            // Sending data to client 2
            ser.setSendPort(9002);  //recport=8002
            ser.setRecPort(8003);  //senport=9003
            System.out.println("Send request data to client2..");
        br=new BufferedReader(new InputStreamReader(System.in));
        String s1=br.readLine();
            System.out.println("Data is "+s1);
            ser.sendData();
            System.out.println("Waiting for response from client2....");
            ser.recData();
           

            //Sending the final result to client 1
            ser.setSendPort(9000);
            ser.sendData();           

            //Sending the final result to client 2
            ser.setSendPort(9002);
            ser.sendData();       
        }
}


class Server
{

    InetAddress lclhost;
    int sendPort,recPort;
    int ssend =0;
    int scounter=0;
    Server(InetAddress lclhost)
    {
        this.lclhost=lclhost;
    }

    public void setSendPort(int sendPort)
    {
        this.sendPort=sendPort;
    }
   
    public void setRecPort(int recPort)
    {
        this.recPort=recPort;
    }

    public void sendData()throws Exception
    {
       
        DatagramSocket ds;
        DatagramPacket dp;
        String data="";

        if(scounter<2 && ssend<2)
            {
                data="VOTE_REQUEST";
            }

        if(scounter<2 && ssend>1)
            {
                data="GLOBAL_ABORT";
                data= data + " TRANSACTION ABORTED";
               
            }
        if(scounter==2 && ssend>1)
            {
                data="GLOBAL_COMMIT";
                data= data + " TRANSACTION COMMITED";

           
            }
       
       
       
        ds=new DatagramSocket(sendPort);
        dp=new DatagramPacket(data.getBytes(),data.length(),lclhost,sendPort-1000);
        ds.send(dp);
        ds.close();
        ssend++;
    }


public void recData()throws Exception
        {
            byte buf[]=new byte[256];
            DatagramPacket dp=null;
            DatagramSocket ds=null;
            String msgStr="";
            try{
            ds=new DatagramSocket(recPort);
            dp=new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
             msgStr=new String(dp.getData(),0,dp.getLength());
System.out.println("String = "+msgStr);           
if(msgStr.equalsIgnoreCase("VOTE_COMMIT"))
                {
                    scounter++;
                }
        System.out.println("Counter value = "+scounter + "\n Send value = "+ssend);
    }       

};

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


public class TPCClient1
{

    public static void main(String a[])throws Exception
        {
            InetAddress lclhost;
            lclhost=InetAddress.getLocalHost();
            Client clnt=new Client(lclhost);

           
            clnt.setSendPort(9001);   //recport=8000
            clnt.setRecPort(8000);   //sendport=9001
            clnt.recData();
            clnt.sendData();
            clnt.recData();
        }
}


class Client
{
InetAddress lclhost;
    int sendPort,recPort;
    Client(InetAddress lclhost)
    {
        this.lclhost=lclhost;
}

    public void setSendPort(int sendPort)
    {
        this.sendPort=sendPort;
    }
   
    public void setRecPort(int recPort)
    {
        this.recPort=recPort;
    }

    public void sendData()throws Exception
    {
        BufferedReader br;
        DatagramSocket ds;
        DatagramPacket dp;
        String data="";
        System.out.println("Enter the Response 'VOTE_COMMIT' || 'VOTE_ABORT' ");
        br=new BufferedReader(new InputStreamReader(System.in));
        data = br.readLine();
        System.out.println("Data is "+data);

       
        ds=new DatagramSocket(sendPort);
        dp=new DatagramPacket(data.getBytes(),data.length(),lclhost,sendPort-1000);
        ds.send(dp);
        ds.close();
       
    }


public void recData()throws Exception
        {
            byte buf[]=new byte[256];
            DatagramPacket dp;
            DatagramSocket ds;
           
            ds=new DatagramSocket(recPort);
            dp=new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();
            String msgStr=new String(dp.getData(),0,dp.getLength());
            System.out.println("Client1 data " +msgStr);
           
    }       

};

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


public class TPCClient2
{

    public static void main(String a[])throws Exception
        {
            InetAddress lclhost;
            lclhost=InetAddress.getLocalHost();
            Client client=new Client(lclhost);

            // Sending data to client 2
            client.setSendPort(9003);  //recport=8002
            client.setRecPort(8002);  //senport=9003
            client.recData();
            client.sendData();
            client.recData();
           
    }
}


class Client
{

    InetAddress lclhost;
    int sendPort,recPort;
    Client(InetAddress lclhost)
    {
        this.lclhost=lclhost;
    }

    public void setSendPort(int sendPort)
    {
        this.sendPort=sendPort;
    }
   
    public void setRecPort(int recPort)
    {
        this.recPort=recPort;
    }

    public void sendData()throws Exception
    {
        BufferedReader br;
        DatagramSocket ds;
        DatagramPacket dp;
        String data="";
        System.out.println("Enter the Response 'VOTE_COMMIT' || 'VOTE_ABORT' ");
        br=new BufferedReader(new InputStreamReader(System.in));
       
        data = br.readLine();
        System.out.println("Data is "+data);

        ds=new DatagramSocket(sendPort);
        dp=new DatagramPacket(data.getBytes(),data.length(),lclhost,sendPort-1000);
        ds.send(dp);
        ds.close();
       
    }

public void recData()throws Exception
        {
            byte buf[]=new byte[256];
            DatagramPacket dp;
            DatagramSocket ds;
            ds=new DatagramSocket(recPort);
            dp=new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();
            String msgStr=new String(dp.getData(),0,dp.getLength());

            System.out.println(msgStr);           
    }       

};

OUTPUT

TPCServer.java

C:\Documents and Settings\Nids\Desktop\DCpracs\twoPhaseCommit_prac7>java TPCServ
er
Server in sending mode.....
Send request data to client1..
VOTE_REQUEST
Data is VOTE_REQUEST
Waiting for response from client1....
String = VORE_COMMIT
Counter value = 0
 Send value = 1
Send request data to client2..
VOTE_REQUEST
Data is VOTE_REQUEST
Waiting for response from client2....
String = VOTE_COMMIT
Counter value = 1
 Send value = 2

TPCClient1.java

C:\Documents and Settings\Nids\Desktop\DCpracs\twoPhaseCommit_prac7>java TPCClie
nt1
VOTE_REQUEST
Enter the Response 'VOTE_COMMIT' || 'VOTE_ABORT'
VORE_COMMIT
Data is VORE_COMMIT
GLOBAL_ABORT TRANSACTION ABORTED

TPCClient2.java

C:\Documents and Settings\Nids\Desktop\DCpracs\twoPhaseCommit_prac7>java TPCClie
nt2
VOTE_REQUEST
Enter the Response 'VOTE_COMMIT' || 'VOTE_ABORT'
VOTE_COMMIT
Data is VOTE_COMMIT
GLOBAL_ABORT TRANSACTION ABORTED

Prof.Mahendra Kanojia

Byzantine General Problem

PROBLEM STATEMENT:

Write a program to implement Byzantine general problem. The program consists of ‘n’, where n > 4, number loyal generals and ’m’, where m <= n, number of disloyal generals. The program produces random values for representing armies of each of the general. The army strength of each general is send to each other general. The list of army strength of every general, except of the current general, is send to every other general for confirmation. Finally, this procedure identifies the disloyal traitor general(s) and displays them.  (In the program a general is represented by a node or terminal.).

DESCRIPTION:

Byzantine Algorithm follows following steps;

Step1:
Every general sends a (reliable) message to every other general announcing his troops. Here loyal generals tell the truth,ehile the traitors may tell every other general a different lie.

Step2:
The results of the announcements of step1 are collected together in the form of the vectors.

Step3:
Every general passing his vector from to every other general.

Step4:
Each general examines the ith element of each of the newly received vectors. If any value has the majority, that value is put into result vector. If no value has a majority, the result vector is marked unknown.


SOURCE CODE:

ByzGenOne.java
import java.io.*;
import java.net.*;
import java.util.*;

public class ByzGenOne
{
     public static void main(String args[])throws Exception
    {
        int icounter;
        InetAddress lclhost;
        GenOne gentwo,genthree,genfour;
       
        for(icounter=0;icounter<2;icounter++)
        {
            lclhost = InetAddress.getLocalHost();
            gentwo = new GenOne(lclhost);
            gentwo.setSendPort(9001);
            gentwo.setRecPort(8002);
            gentwo.sendData();
            gentwo.recData();
           
           
            lclhost = InetAddress.getLocalHost();
            genthree = new GenOne(lclhost);
            genthree.setSendPort(9003);
            genthree.setRecPort(8004);
            genthree.sendData();
            genthree.recData();
       
           
            lclhost = InetAddress.getLocalHost();
            genfour = new GenOne(lclhost);
            genfour.setSendPort(9005);
            genfour.setRecPort(8006);
            genfour.sendData();
            genfour.recData();
           
            GenOne.vecdata = true;
        }
    }

}

class GenOne
{
    InetAddress lclhost;
    int sendport,recport;
    private static String vecstr="",datastr="";
    static boolean vecdata = false; //Used to send received armies' strength
    private static int recctr = 0;  //No of received msgs
    private static int ctr=0;
    private static int fnctr = 0;
    private static int recdata=0;
    static int dataarr[] = new int[12];
    static int finalarr[] = new int[3];
    GenOne(InetAddress lclhost)
        {
            this.lclhost = lclhost;
        }
       
   
    void setSendPort(int sendport)
        {
            this.sendport = sendport;
        }
       
    void setRecPort(int recport)
        {
            this.recport = recport;
        }

    void  sendData()throws Exception
        {   
             DatagramSocket ds;
             DatagramPacket dp;
            BufferedReader br;
           
            System.out.println("Enter the Data");
           
            if(vecdata == false)
            {
br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
            }
            else
            {
            br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
                datastr = vecstr;
            }
            ds = new DatagramSocket(sendport);
            dp = new DatagramPacket(datastr.getBytes(),datastr.length(),lclhost,sendport-1000);
            ds.send(dp);
            ds.close();
        }

        void recData()throws Exception
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte[]  buf = new byte[256];
             String msgstr;

            ds = new DatagramSocket(recport);
            dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();

            msgstr = new String(dp.getData(),0,dp.getLength());
            System.out.println(msgstr);
            int l=msgstr.length();
            System.out.println("Hello");
            System.out.println("Length =  "+l);
            if(msgstr.length() == 9)
            {
                recctr++;   
                recdata = Integer.parseInt(msgstr);
                dataarr[ctr++] = recdata/1000000;       
               
                recdata  = recdata % 1000000;
                dataarr[ctr++] = recdata / 1000;

                recdata = recdata %1000;
                dataarr[ctr++] = recdata;
               
                if(recctr == 3)
                    maxval();
            }
           
            if(vecdata == false)
            {
                vecstr = vecstr.concat(msgstr);
               
                System.out.println("Vector Data "+vecstr);
            }
        }           
           

        void  maxval()
        {
              int ctr1,ctr2,i,j;
            boolean gentwo=false,genthree=false,genfour=false;
           
            for(ctr1=0;ctr1<9;ctr1++)
                {
                    i=0;
                    j = dataarr[ctr1];
                   
                    if(dataarr[ctr1]!=0)
                    {
                        //System.out.println("found");
                        for(ctr2=0;ctr2<9;ctr2++)
                        {
                            if(j==dataarr[ctr2])
                            {
                                i++;
                                dataarr[ctr2] =0;
                            }
                           
                        }
                    if(i==2)
                   
                        finalarr[fnctr++] = j;
                }   
            }
Arrays.sort(finalarr);
                System.out.println("Final Vector");
                   
                for(ctr1 = 0 ;ctr1<3 ; ctr1++)
                    System.out.println(finalarr[ctr1] + " ");
               
                for(ctr1 =0 ; ctr1<3 ; ctr1++)
                {
                if(finalarr[ctr1] >=200 && finalarr[ctr1] <300)
                {
                System.out.println("General Number two is
                Loyal");

                gentwo = true;
                }
               
            else if(finalarr[ctr1] >=300 && finalarr[ctr1] <400)
                {
                 System.out.println("General Number three is
                 Loyal");
                 genthree = true;
                }

            else if(finalarr[ctr1] >=400 && finalarr[ctr1] <500)
                {
                System.out.println("General Number four is
                Loyal");
genfour = true;
                }

           
                   
                    }
            if(gentwo ==false)
            System.out.println("General Number Two is Not Loyal");
           
            if(genthree ==false)
            System.out.println("General Number Three is Not
            Loyal");
           
            if(genfour ==false)                                        System.out.println("General Number Four is Not
            Loyal");

            }

        }

ByzGenTwo.java
import java.io.*;
import java.net.*;
import java.util.*;

public class ByzGenTwo
{
    public static void main(String args[])throws Exception
    {
        int icounter;
        InetAddress lclhost;
        GenTwo gentwo,genthree,genfour;
       
        for(icounter=0;icounter<2;icounter++)
        {
            lclhost = InetAddress.getLocalHost();
            gentwo = new GenTwo(lclhost);
            gentwo.setSendPort(9002);
            gentwo.setRecPort(8001);
           
            gentwo.recData();
            gentwo.sendData();
           
            lclhost = InetAddress.getLocalHost();
            genthree = new GenTwo(lclhost);
            genthree.setSendPort(9007);
            genthree.setRecPort(8008);
            genthree.sendData();
            genthree.recData();
       
           
            lclhost = InetAddress.getLocalHost();
            genfour = new GenTwo(lclhost);
            genfour.setSendPort(9009);
            genfour.setRecPort(8010);
            genfour.sendData();
            genfour.recData();
           
            GenTwo.vecdata = true;
        }
    }

}

class GenTwo
{
    InetAddress lclhost;
    int sendport,recport;
    private static String vecstr = "",datastr="";
    static boolean vecdata = false; //Used to send received armies' strength
    private static int recctr = 0;  //No of received msgs
    private static int ctr=0;
    private static int fnctr = 0;
    private static int recdata=0;
    static int dataarr[] = new int[12];
    static int finalarr[] = new int[3];
    GenTwo(InetAddress lclhost)
        {
            this.lclhost = lclhost;
        }
       
   
    void setSendPort(int sendport)
        {
            this.sendport = sendport;
        }
       
    void setRecPort(int recport)
        {
            this.recport = recport;
        }

    void  sendData()throws Exception
        {   
             DatagramSocket ds;
             DatagramPacket dp;
            BufferedReader br;
   
            ds = new DatagramSocket(sendport);
           
            System.out.println("Enter the Data");
           
            if(vecdata == false)
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
            }
            else
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
                datastr = vecstr;
            }

            dp = new DatagramPacket(datastr.getBytes(),datastr.length(),lclhost,sendport-1000);
            ds.send(dp);
            ds.close();
        }

        void recData()throws Exception
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte[]  buf = new byte[256];
             String msgstr;

            ds = new DatagramSocket(recport);
            dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();

            msgstr = new String(dp.getData(),0,dp.getLength());
            System.out.println(msgstr);

            if(msgstr.length() == 9)
            {
                recctr++;   
                recdata = Integer.parseInt(msgstr);
                dataarr[ctr++] = recdata/1000000;       
               
                recdata  = recdata % 1000000;
                dataarr[ctr++] = recdata/1000;

                recdata = recdata %1000;
                dataarr[ctr++] = recdata;
               
                if(recctr == 3)
                    maxval();
            }
           
            if(vecdata ==false)
            {
                vecstr = vecstr.concat(msgstr);
                System.out.println("Vector Data "+vecstr);
            }
        }           
           

        void  maxval()
        {
             int ctr1,ctr2,i,j;
            boolean gentwo=false,genthree=false,genfour=false;
           
            for(ctr1=0;ctr1<9;ctr1++)
                {
                    i=0;
                    j = dataarr[ctr1];
                    if(dataarr[ctr1]!=0)
                    {
                        for(ctr2=0;ctr2<9;ctr2++)
                        {
                            if(j==dataarr[ctr2])
                            {
                                i++;
                                dataarr[ctr2] =0;
                            }
                        }
                   
                    if(i==2)
                   
                        finalarr[fnctr++] = j;
                    }
                }   
           
                Arrays.sort(finalarr);
                System.out.println("Final Vector");
                   
                for(ctr1 = 0 ;ctr1<3 ; ctr1++)
                    System.out.println(finalarr[ctr1] + " ");
               
                for(ctr1 =0 ; ctr1<3 ; ctr1++)
                    {
                if(finalarr[ctr1] >=100 && finalarr[ctr1] <200)
                {
            System.out.println("General Number One is
            Loyal");

                gentwo = true;
                }
               
            else if(finalarr[ctr1] >=300 && finalarr[ctr1] <400)
            {
            System.out.println("General Number three is Loyal");
            genthree = true;
            }

            else if(finalarr[ctr1] >=400 && finalarr[ctr1] <500)
                        {
            System.out.println("General Number four is Loyal");
            genfour = true;
            }
}
            if(gentwo ==false)
            System.out.println("General Number One is Not Loyal");
           
            if(genthree ==false)
            System.out.println("General Number Three is Not
            Loyal");

            if(genfour ==false)                                          System.out.println("General Number Four is Not                     Loyal");

}

        }

ByzGenThree.java
import java.io.*;
import java.net.*;
import java.util.*;

public class ByzGenThree
{
    public static void main(String args[])throws Exception
    {
        int icounter;
        InetAddress lclhost;
        GenThree gentwo,genthree,genfour;
       
        for(icounter=0;icounter<2;icounter++)
        {
            lclhost = InetAddress.getLocalHost();
            gentwo = new GenThree(lclhost);
            gentwo.setSendPort(9004);
            gentwo.setRecPort(8003);
            gentwo.recData();
            gentwo.sendData();
           
            lclhost = InetAddress.getLocalHost();
            genthree = new GenThree(lclhost);
            genthree.setSendPort(9008);
            genthree.setRecPort(8007);
            genthree.recData();
            genthree.sendData();
           
            lclhost = InetAddress.getLocalHost();
            genfour = new GenThree(lclhost);
            genfour.setSendPort(9011);
            genfour.setRecPort(8012);
            genfour.sendData();
            genfour.recData();
           
            GenThree.vecdata = true;
        }
    }

}

class GenThree
{
    InetAddress lclhost;
    int sendport,recport;
    private static String vecstr = "",datastr="";
    static boolean vecdata = false; //Used to send received armies' strength
    private static int recctr = 0;  //No of received msgs
    private static int ctr=0;
    private static int fnctr = 0;
    private static int recdata=0;
    private static int dataarr[] = new int[12];
    private static int finalarr[] = new int[3];
    GenThree(InetAddress lclhost)
        {
            this.lclhost = lclhost;
        }
       
   
    void setSendPort(int sendport)
        {
            this.sendport = sendport;
        }
       
    void setRecPort(int recport)
        {
            this.recport = recport;
        }

    void  sendData()throws Exception
        {   
             DatagramSocket ds;
             DatagramPacket dp;
            BufferedReader br;
   
            ds = new DatagramSocket(sendport);
           
            System.out.println("Enter the Data");
           
            if(vecdata == false)
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
            }
            else
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
                datastr = vecstr;
            }

            dp = new DatagramPacket(datastr.getBytes(),datastr.length(),lclhost,sendport-1000);
            ds.send(dp);
            ds.close();
        }

        void recData()throws Exception
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte[]  buf = new byte[256];
             String msgstr;

            ds = new DatagramSocket(recport);
            dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();

            msgstr = new String(dp.getData(),0,dp.getLength());
            System.out.println(msgstr);

            if(msgstr.length() == 9)
            {
                recctr++;   
                recdata = Integer.parseInt(msgstr);
                dataarr[ctr++] = recdata/1000000;       
               
                recdata  = recdata % 1000000;
                dataarr[ctr++] = recdata / 1000;

                recdata = recdata %1000;
                dataarr[ctr++] = recdata;
               
                if(recctr == 3)
                    maxval();
            }
           
            if(vecdata ==false)
            {
                vecstr = vecstr.concat(msgstr);
                System.out.println("Vector Data "+vecstr);
            }
        }           
           

        void  maxval()
        {
             int ctr1,ctr2,i,j;
            boolean gentwo=false,genthree=false,genfour=false;
           
            for(ctr1=0;ctr1<9;ctr1++)
                {
                    i=0;
                    j = dataarr[ctr1];
                    if(dataarr[ctr1]!=0)
                    {
                        for(ctr2=0;ctr2<9;ctr2++)
                        {
                            if(j==dataarr[ctr2])
                            {
                                i++;
                                dataarr[ctr2] =0;
                            }
                        }
                   
                    if(i==2)
                   
                        finalarr[fnctr++] = j;
                    }
                }   
           
                Arrays.sort(finalarr);
                System.out.println("Final Vector");
                   
                for(ctr1 = 0 ;ctr1<3 ; ctr1++)
                    System.out.println(finalarr[ctr1] + " ");
               
                for(ctr1 =0 ; ctr1<3 ; ctr1++)
                {
                if(finalarr[ctr1] >=100 && finalarr[ctr1] <200)
                {
                System.out.println("General Number one is
                Loyal");
                gentwo = true;
                }
               
            else if(finalarr[ctr1] >=200 && finalarr[ctr1] <300)
                {
                System.out.println("General Number two is
                Loyal");
                genthree = true;
                }

            else if(finalarr[ctr1] >=400 && finalarr[ctr1] <500)
            {
                System.out.println("General Number four is
                Loyal");
                genfour = true;
            }

        }
            if(gentwo ==false)
            System.out.println("General Number One is Not Loyal");
           
            if(genthree ==false)
            System.out.println("General Number Two is Not Loyal");
           
             if(genfour ==false)                                        System.out.println("General Number Four is Not
            Loyal");
}
        }

ByzGenFour.java
import java.io.*;
import java.net.*;
import java.util.*;

public class ByzGenFour
{
    public static void main(String args[])throws Exception
    {
        int icounter;
        InetAddress lclhost;
        GenFour gentwo,genthree,genfour;
       
        for(icounter=0;icounter<2;icounter++)
        {
            lclhost = InetAddress.getLocalHost();
            gentwo = new GenFour(lclhost);
            gentwo.setSendPort(9006);
            gentwo.setRecPort(8005);
           
            gentwo.recData();
            gentwo.sendData();
           
            lclhost = InetAddress.getLocalHost();
            genthree = new GenFour(lclhost);
            genthree.setSendPort(9010);
            genthree.setRecPort(8009);
           
            genthree.recData();
            genthree.sendData();
           
            lclhost = InetAddress.getLocalHost();
            genfour = new GenFour(lclhost);
            genfour.setSendPort(9012);
            genfour.setRecPort(8011);
           
            genfour.recData();
            genfour.sendData();
            GenFour.vecdata = true;
        }
    }

}

class GenFour
{
    InetAddress lclhost;
    int sendport,recport;
    private static String vecstr = "",datastr="";
    static boolean vecdata = false; //Used to send received armies' strength
    private static int recctr = 0;  //No of received msgs
    private static int ctr=0;
    private static int fnctr = 0;
    private static int recdata=0;
    static int dataarr[] = new int[12];
    static int finalarr[] = new int[3];
    GenFour(InetAddress lclhost)
        {
            this.lclhost = lclhost;
        }
       
   
    void setSendPort(int sendport)
        {
            this.sendport = sendport;
        }
       
    void setRecPort(int recport)
        {
            this.recport = recport;
        }

    void  sendData()throws Exception
        {   
             DatagramSocket ds;
             DatagramPacket dp;
            BufferedReader br;
   
            ds = new DatagramSocket(sendport);
           
            System.out.println("Enter the Data");
           
            if(vecdata == false)
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
            }
            else
            {
                br = new BufferedReader(new InputStreamReader(
System.in));
                datastr = br.readLine();
                datastr = vecstr;
            }

            dp = new DatagramPacket(datastr.getBytes(),datastr.length(),lclhost,sendport-1000);
            ds.send(dp);
            ds.close();
        }

        void recData()throws Exception
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte[]  buf = new byte[256];
             String msgstr;

            ds = new DatagramSocket(recport);
            dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            ds.close();

            msgstr = new String(dp.getData(),0,dp.getLength());
            System.out.println(msgstr);

            if(msgstr.length() == 9)
            {
                recctr++;   
                recdata = Integer.parseInt(msgstr);
                dataarr[ctr++] = recdata/1000000;       
               
                recdata  = recdata % 1000000;
                dataarr[ctr++] = recdata / 1000;

                recdata = recdata %1000;
                dataarr[ctr++] = recdata;
               
                if(recctr == 3)
                    maxval();
            }
           
            if(vecdata ==false)
            {
                vecstr = vecstr.concat(msgstr);
                System.out.println("Vector Data "+vecstr);
            }
        }           
           

        void  maxval()
        {
             int ctr1,ctr2,i,j;
            boolean gentwo=false,genthree=false,genfour=false;
           
            for(ctr1=0;ctr1<9;ctr1++)
                {
                    i=0;
                    j = dataarr[ctr1];
                    if(dataarr[ctr1]!=0)
                    {
                        for(ctr2=0;ctr2<9;ctr2++)
                        {
                            if(j==dataarr[ctr2])
                            {
                                i++;
                                dataarr[ctr2] =0;
                            }
                        }
                   
                    if(i==2)
                   
                        finalarr[fnctr++] = j;
                    }
                }   
           
                Arrays.sort(finalarr);
                System.out.println("Final Vector");
                   
                for(ctr1 = 0 ;ctr1<3 ; ctr1++)
                    System.out.println(finalarr[ctr1] + " ");
               
                for(ctr1 =0 ; ctr1<3 ; ctr1++)
                    {
                if(finalarr[ctr1] >=100 && finalarr[ctr1] <200)
                {
                System.out.println("General Number one is
                Loyal");
                gentwo = true;
                }
   
        else if(finalarr[ctr1] >=200 && finalarr[ctr1] <300)
            {
            System.out.println("General Number two is Loyal");
            genthree = true;
        }

        else if(finalarr[ctr1] >=300 && finalarr[ctr1] <400)
            {
                System.out.println("General Number three is
                Loyal")
genfour = true;
            }

            }
            if(gentwo ==false)
             System.out.println("General Number one is Not Loyal");
           
            if(genthree ==false)
             System.out.println("General Number Two is Not Loyal");
           
            if(genfour ==false)                                        System.out.println("General Number three is Not
            Loyal");
   
            }

        }

OUTPUT

ByzGenOne.java

C:\Documents and Settings\Admin\Desktop\Byzantine>java ByzGenOne
Enter the Data
111
222
Hello
Length =  3
Vector Data 222
Enter the Data
111
333
Hello
Length =  3
Vector Data 222333
Enter the Data
111
444
Hello
Length =  3
Vector Data 222333444
Enter the Data
1
111333444
Hello
Length =  9
Enter the Data
1
111222444
Hello
Length =  9
Enter the Data
1
111222333
Hello
Length =  9
Final Vector
222
333
444
General Number two is Loyal
General Number three is Loyal
General Number four is Loyal

ByzGenTwo.java

C:\Documents and Settings\Admin\Desktop\Byzantine>java ByzGenTwo
111
Vector Data 111
Enter the Data
222
Enter the Data
222
333
Vector Data 111333
Enter the Data
222
444
Vector Data 111333444
222333444
Enter the Data
111333444
Enter the Data
2
111222444
Enter the Data
2
111222333
Final Vector
111
333
444
General Number One is Loyal
General Number three is Loyal
General Number four is Loyal

ByzGenThree.java

C:\Documents and Settings\Admin\Desktop\Byzantine>java ByzGenThree
111
Vector Data 111
Enter the Data
333
222
Vector Data 111222
Enter the Data
333
Enter the Data
333
444
Vector Data 111222444
222333444
Enter the Data
111222444
111333444
Enter the Data
111222444
Enter the Data
3
111222333
Final Vector
111
222
444
General Number one is Loyal
General Number two is Loyal
General Number four is Loyal

ByzGenFour.java

C:\Documents and Settings\Admin\Desktop\Byzantine>java ByzGenFour
111
Vector Data 111
Enter the Data
444
222
Vector Data 111222
Enter the Data
444
333
Vector Data 111222333
Enter the Data
444
222333444
Enter the Data
111222333
111333444
Enter the Data
111222333
111222444
Final Vector
111
222
333
General Number one is Loyal
General Number two is Loyal
General Number three is Loyal
Enter the Data
111222333
Prof.Mahendra Kanojia