sivaramaiah  
 
  Record 5 03/29/2024 12:59am (UTC)
   
 
 
Record 5:Phone book server:
Create a remote phone book server that maintains names and phone numbers. Phone book client should provide a user interface that allows the user to scroll through entries, add a new entry, modify an existing entry and delete an existing entry. The client and the server should provide proper error handling.

-----------------------------------------------------------------------------------------

Program 1: Interface program                          phonebookInt.java
import java.rmi.*;
import java.rmi.Remote.*;

public interface phonebookInt extends Remote
{
    public String loadPhonebook() throws RemoteException;
    public boolean savePhonebook(String[] phoneEntries) throws RemoteException;
}
Program 2: Implementation program                            phonebookImp.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;

public class phonebookImp extends UnicastRemoteObject implements phonebookInt
{
    public phonebookImp() throws RemoteException
    {
        super();
    }
    
    public String loadPhonebook() throws RemoteException
    {
    try{
        FileInputStream fis = new FileInputStream("phonebook.txt");
        byte buffer[] = new byte[2056];
        int fsize= fis.read(buffer,0,2056);
        fis.close();
        String str = new String(buffer,0,fsize,"Default");
        return str;
       }
       catch(Exception fe) { System.out.println("Error : "+fe); return null; }
    }
    
    public boolean savePhonebook(String[] phoneEntries) throws RemoteException
    {
        try
        {    FileOutputStream fos = new FileOutputStream("phonebook.txt",false);
            for(int c=0;c<phoneEntries.length;c++)
            fos.write((phoneEntries[c]+",").getBytes());
            fos.close();
            return true;
        }
        catch(Exception fe) { System.out.println("Error : "+fe); return false; }
    }
}
-------------------------------------------------------------
Program 3: Server program                                               phonebookServer.java
import java.rmi.*;
import java.rmi.server.*;

public class phonebookServer
{
    public static void main(String[] args)
    {
        try
        {
        System.out.print("Constructing Server Implementation...");
        phonebookImp Pbook1 = new phonebookImp();
        Naming.rebind("PhoneBook",Pbook1);
        System.out.print("Done!nWaiting for Invocations from client...n");
        }
        catch(Exception e)
        { System.out.print("Error at phonebookServer : "+e);
        }
    }
}


Program 4: Client program                                                phonebookClient.java
import java.rmi.*;
import java.rmi.server.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
import java.io.*;

public class phonebookClient
{
    static JTable jtab;
    static JFrame frame;
    static DefaultTableModel dTableModel;
    static int SIZE;
    public static void main(String[] args) throws IOException
    {
        phonebookInt pBookobj;
        frame = new JFrame("Phone Book (RMI)");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String headers[] = {"Name","Phone"};
        
        System.out.print("Naming.lookup for Bindings...");
    
        try {
            pBookobj = (phonebookInt) Naming.lookup("PhoneBook");
            System.out.print("Done!n");
            /*-------- invoke server method: byte[] loadPhonebook()-----------*/
            String str = pBookobj.loadPhonebook();
            String[] content = str.split(",");
            SIZE =content.length/2;
            String[][] data = new String[SIZE][2];
            for(int i=0,j=0;i<content.length;i+=2,j++)
            {
                data[j][0] =content[i].toString();
                data[j][1]=content[i+1].toString();
            }
    
            dTableModel = new DefaultTableModel();
            dTableModel.setDataVector(data, headers);
    
            jtab = new JTable(dTableModel);
            ListSelectionModel lsm = jtab.getSelectionModel();
            lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
            JScrollPane jscrolltab = new JScrollPane(jtab);
        
            jscrolltab.setAutoscrolls(true);
            jscrolltab.setPreferredSize(new Dimension(400,200));
            JPanel phonetab = new JPanel();
            JPanel buttons = new JPanel();
            phonetab.setBorder(BorderFactory.createTitledBorder("Phone Book"));
            phonetab.add(jscrolltab);
            JButton b1 = new JButton("New");
            JButton b2 = new JButton("Save");
            JButton b3 = new JButton("Delete");
    
            buttonListener bl1 = new buttonListener();
            b1.addActionListener(bl1);
            b2.addActionListener(bl1);
            b3.addActionListener(bl1);
    
            buttons.add(b1);
            buttons.add(b2);
            buttons.add(b3);
            phonetab.add(buttons);
            frame.add(phonetab);
            frame.setSize(425,325);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
            }
            catch(Exception nbe) { System.out.print("Error : "+nbe); System.exit(0);}
    }

}

class buttonListener implements ActionListener
{
    DefaultTableModel pbtable = phonebookClient.dTableModel;
    public void actionPerformed(ActionEvent e)
    {    
        phonebookInt pBookobj;
        JButton jb =(JButton)e.getSource();
        int rc=pbtable.getRowCount();
        int cc=pbtable.getColumnCount();
        int totalcells = rc*cc;
        boolean status=false;
        if(jb.getText().equals("New"))
        {
            String[] newrow = {"",""};
            pbtable.addRow(newrow);    
        }
        if(jb.getText().equals("Save"))
        {String phdata[] = new String[totalcells];
        int i=0,j=0,k=0;
            try {
                for(i=0;i<rc;i++)
                for(j=0;j<cc;j++)
                {
                    String str = (String)pbtable.getValueAt(i,j);
                    if(str.trim()=="")
                    {
                        JOptionPane.showMessageDialog(phonebookClient.frame, "Incomplete PhoneBook Entries Found.nPlease Fill in before save.", "Save Failed", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    else
                    {phdata[k++]=str; }
                }
            } catch(Exception e1) {System.out.print("AIOB Exception:nError="+e1); }
        System.out.print("Naming.lookup for Bindings...");
        try {
            pBookobj = (phonebookInt) Naming.lookup("PhoneBook");
            System.out.print("Done!n");
            /*-------- invoke server method: boolean savePhonebook(String[] )-----------*/
            status = pBookobj.savePhonebook(phdata);
            if(status)
                JOptionPane.showMessageDialog(phonebookClient.frame, "PhoneBook Saved Successfully.", "Save Complete", JOptionPane.INFORMATION_MESSAGE);
            else
            JOptionPane.showMessageDialog(phonebookClient.frame, "PhoneBook save failed.", "Saving failed", JOptionPane.ERROR_MESSAGE);
            } catch(Exception nbe) { System.out.print("Error: "+nbe); System.exit(0); }
        }
        if(jb.getText().equals("Delete"))
        {
            int choice = JOptionPane.showConfirmDialog(phonebookClient.frame,"Are you sure?","Confirm Delete",JOptionPane.YES_NO_OPTION);
            if(choice==JOptionPane.YES_OPTION)
            {
            try{
                pbtable.removeRow(phonebookClient.jtab.getSelectedRow());
            }
            catch(Exception err) {System.out.println("Maybe you forgot to select a row before removing");  }
            }
        }
    }
}
------------------------------------------------------------
Program 5: Show Bindings                                                  showBingings.java
import java.rmi.*;
import java.rmi.server.*;
 
public class showBindings
{
            public static void main(String[] args)
            {
                        try
                        {
                                    String[] bindings = Naming.list("");
                                    for(int i=0;i<bindings.length;i++)
                                    System.out.println("Binding ["+(i+1)+"] => "+bindings[i]);
                        }
                        catch(Exception e)
                        { System.out.println("Error in showBindings : "+e.toString());     }
                       
            }
}
Program6:clinet.policy                                    clinet.policy
grant
{
            permission java.net.SocketPermission
            "*:1024-65335","connect";
};
How to run the program
1. compile all java files
 
            javac *.java
 
2. use RMICompiler to create stubs & skeletons of Implementations
           
            rmic phonebookImp
 
3. run rmiregistry
           
            start rmiregistry
 
4. create policy file with following contents
 
            client.policy
            ~~~~~~~~~~~~~
           
            grant
            {
                        permission java.net.SocketPermission
                        "*:1024-65335","connect";
            };
 
5. run server
 
            java phonebookServer
 
6. run client with policy created in step 4.
 
            java -Djava.security.policy=client.policy phonebookClient
 
 


 
  Menu Items
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  add
HOT TOPICS
MCA PROJECT DETAILS ------------------------------------- Jntu all Lab manuals ------------------------------------- Jntu Syllabus Books ------------------------------------- Paper presentations ------------------------------------- Seminars ------------------------------------- Campus Papers ------------------------------------- Competetive Exams GATE ------------------------------------- GRE ------------------------------------- TOEFL ------------------------------------- IELTS ------------------------------------- CAT ------------------------------------- GMAT ------------------------------------- Templates ------------------------------------- Students Resume Preparation tips ------------------------------------- Job zone(Interview questions) ------------------------------------- Google Adsence html code ------------------------------------- Web sites --------x--------------x-----------
  Advertisement

 


-----------------------------
  Offline Messages
  Adds
  Visitors Information
Today, there have been 118772 visitors (279440 hits) on this page!
-------------------------------------------------- This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free