Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » CorbaRSS Feeds

Program that takes a long sentence and a character from the client and count the occurrence of the character in the sentence at the server side

Posted By: Abbey Fischer     Category: Java     Views: 2608

Write a CORBA program that takes a long sentence and a character from the client and count the occurrence of the character in the sentence at the server side and then prints the count on client machine.

Code for Program that takes a long sentence and a character from the client and count the occurrence of the character in the sentence at the server side in Java

----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CountSentApp
{
    interface CountSent
    {
        long CountOccu(instring s,inchar c);
        oneway void shutdown();
    };
    
};

----------------------------------------------------------------------------------------------
                    Server
----------------------------------------------------------------------------------------------

import CountSentApp.*;
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;

class CountSentImpl extends CountSentPOA
{
    private ORB orb;

    publicvoid setORB(ORB orb_val)
    {
        orb = orb_val; 
      }
    publicint CountOccu(String s, char c)
    {
        int count=0;
        for (int i = 0; i < s.length(); i++)
        {
            if (c == s.charAt(i))
                count++;
        }
        return count;
    }
    publicvoid shutdown()
    {
        orb.shutdown(false);
    }
}

publicclass CountSentServer
{

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

            CountSentImpl countSentImpl = new CountSentImpl();
            countSentImpl.setORB(orb);

            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(countSentImpl);
            CountSent href = CountSentHelper.narrow(ref);
    
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

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

            System.out.println("CountSentServer ready and waiting ...");

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

        System.out.println("CountSentServer Exiting ...");
    
    }
}

----------------------------------------------------------------------------------------------
                    Client
----------------------------------------------------------------------------------------------
import CountSentApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

publicclass CountSentClient
{
    public String getText() throws IOException
    {
        String s = new String();
        try
        {
            System.out.print("Enter the Text : ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            s = br.readLine();
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
        return s;
    }
    publicstaticvoid main(String args[])
    {
        CountSent countSentImpl = null;
        CountSentClient cc = new CountSentClient();

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

                countSentImpl = CountSentHelper.narrow(ncRef.resolve_str(name));

            String s = cc.getText();
            char c = 's';

            System.out.println("The character is : s");
            int count = countSentImpl.CountOccu(s,c);
            System.out.println("The total occurance is : " + count);

                countSentImpl.shutdown();

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

}

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

Enter the Text : This is the sentence
The character is : s
The total occurance is : 3
  
Share: 



Abbey Fischer
Abbey Fischer author of Program that takes a long sentence and a character from the client and count the occurrence of the character in the sentence at the server side is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!