Java Tutorial

 
 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module DateTimeApp
{
    interface DateTime
    {
        string ServerDT();
        string DateDiff(in long dd,in long mm,in long yy);
        string TimeDiff(in long hh,in long min,in long sec);
        oneway void shutdown();
    };

};
-------------------------------------------------------------------------------------------------
                    Server
-------------------------------------------------------------------------------------------------
import DateTimeApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;
import java.util.*;

class DateTimeImpl extends DateTimePOA
{
    private ORB orb;

    public void setORB(ORB orb_val)
    {
        orb = orb_val; 
      }
    public String ServerDT()
    {
        Date d = new Date ();
        String s = d.toString();
        return s;
    }
    public String DateDiff(int dd,int mm,int yy)
    {
        Calendar cal = Calendar.getInstance();
        int d = cal.get(Calendar.DAY_OF_MONTH) - dd;
        int m = cal.get(Calendar.MONTH) - mm;
        int y = cal.get(Calendar.YEAR) - yy;
        String dt = d + \" / \" + m + \" / \" + y;
        return dt;
    }
    public String TimeDiff(int hh, int min, int sec)
    {
        Calendar cal = Calendar.getInstance();
        int h = cal.get(Calendar.HOUR)- hh;
        int m = cal.get(Calendar.MINUTE) - min;
        int s = cal.get(Calendar.SECOND) - sec;
        String tm = h + \"  \" + m + \"  \" + s;
        return tm;
    }
    public void shutdown()
    {
        orb.shutdown(false);
    }
}

public class DateTimeServer
{

    public static void main(String args[]) 
    {
        try
        {
            ORB orb = ORB.init(args, null);
            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references(\"RootPOA\"));
            rootpoa.the_POAManager().activate();

            DateTimeImpl dateTimeImpl = new DateTimeImpl();
            dateTimeImpl.setORB(orb);

            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(dateTimeImpl);
            DateTime href = DateTimeHelper.narrow(ref);

            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            String name = \"DateTime\";
            NameComponent path[] = ncRef.to_name( name );
            ncRef.rebind(path, href);

            System.out.println(\"DateTimeServer ready and waiting ...\");

            orb.run();
        } 

        catch (Exception e) 
        {
            System.err.println(\"ERROR: \" + e);
            e.printStackTrace(System.out);
        }

        System.out.println(\"DateTimeServer Exiting ...\");

    }
}
-------------------------------------------------------------------------------------------------
                    Client
-------------------------------------------------------------------------------------------------

import DateTimeApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
import java.util.*;

public class DateTimeClient
{
    public void showClientDateTime()
    {
        Date d = new Date();
        String s = d.toString();
        System.out.println(\"Client Date nad Time is : \"+s);
    }

    public static void main(String args[])
    {

        DateTime dateTimeImpl = null;
        DateTimeClient dt = new DateTimeClient();

        try
        {
            ORB orb = ORB.init(args, null);
            org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            String name = \"DateTime\";
            dateTimeImpl = DateTimeHelper.narrow(ncRef.resolve_str(name));

            int dd = 0, mm = 0, yy = 0, hh = 0, min = 0, sec = 0;

              dt.showClientDateTime();

            String s = dateTimeImpl.ServerDT();
            System.out.println(\"Server Date nad Time is : \"+s);


            Calendar cal = Calendar.getInstance();

            dd = cal.get(Calendar.DAY_OF_MONTH);
            mm = cal.get(Calendar.MONTH);
            yy = cal.get(Calendar.YEAR);

            hh = cal.get(Calendar.HOUR);
            min = cal.get(Calendar.MINUTE);
            sec = cal.get(Calendar.SECOND);

            String dateD = dateTimeImpl.DateDiff(dd, mm, yy);

            String timeD = dateTimeImpl.TimeDiff(hh, min, sec);

            System.out.println(\"The date difference of Server Client is \" + dateD);
            System.out.println(\"The time difference of Server Client is \" + timeD);

            dateTimeImpl.shutdown();

        } 
        catch (Exception e) 
        {
            System.out.println(\"ERROR : \" + e) ;
            e.printStackTrace(System.out);
        }
    }

}

----------------------------------------------------------------------------------
                    Output
----------------------------------------------------------------------------------

Client Date nad Time is : Fri Dec 03 13:46:08 GMT+05:30 2004
Server Date nad Time is : Fri Dec 03 13:46:08 GMT+05:30 2004
The date difference of Server Client is 0 / 0 / 0
The time difference of Server Client is 0  0  0

    Related Post:
  1. Program to read the ballots and count the votes cast for each candidate using an array variable count

  2. Program to show an example of extracting subStrings from a String

  3. Program to show the use of If-Else Statement

  4. Program to show an example of using a Static Nested Class outside the Top-Level Class

  5. Program to show the use of Conditional AND Operator (&&) in If Statements

  6. Program that will count the number of lines in each file that is specified on the command line

  7. Program which creates an Array of character. Make one function with one argument as a character and that function throw a user defined exception

  8. Program to determine the sum of the following harmonic series for a given value of n

  9. Even no or Prime no?????

  10. Servlet which takes input from an HTML client page and calculates and outputs the premium amount

  11. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  12. An applet program to give demo of getDocumentBase() and getCodeBase() methods

  13. Application which takes your marks of six subjects from the client side and displays the percentage that you have obtained

  14. Program of Floyds Triangle

  15. CORBA program for displaying the date and time of the server machine, client machine and the difference between these two date and time

  16. RMI program of count server and client

  17. Program to append a StringBuffer by StringBuffer, String objects and other Basic Data Types

  18. Program to declare and initialize Integer variables and print them on the Standard Output Device

  19. Program to show an example of Driving and using a Class

  20. Program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7