Skip to main content

FACTORIAL OF A NUMBER USING RMI IN JAVA



CLIENT PROGRAM:

import java.io.*;

import java.rmi.*;

public class client

{

public static void main(String args[])throws Exception

{

try

{

String s="rmi://"+args[0]+"/abc";

serverint f=(serverint)Naming.lookup(s);

DataInputStream m=new DataInputStream(System.in);

int n1=Integer.parseInt(m.readLine());

System.out.println("the factorial is"+f.fact(n1));

}

catch(Exception e)

{

System.out.println(e);

}

}

}









INTERFACE PROGRAM:

import java.rmi.*;

public interface serverint extends Remote

{

int fact(int n)throws Exception;

}



IMPLEMENTATION PROGRAM:

import java.rmi.*;

import java.rmi.server.*;

public class serverimpl extends UnicastRemoteObject implements serverint

{

public serverimpl()throws Exception

{

}

public int fact(int n)

{

int i,c=1;

for(i=1;i<=n;i++)

{

c=i*c;

}

return c;

}

}



SERVER PROGRAM:

import java.net.*;

import java.rmi.*;

public class server

{

public static void main(String args[])

{

try

{

serverimpl m=new serverimpl();

Naming.rebind("abc",m);

}

catch(Exception e)

{

System.out.println("Exception"+e);

}

}

}









OUTPUT:



SERVER WINDOW:

 C:\>javac serverint.java

 C:\>javac serverimpl.java

 C:\>javac server.java

 C:\>rmic serverimpl

 C:\>start rmiregistry

 C:\>java server



 CLIENT WINDOW:

 C:\>javac client.java

Note: client.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

 C:\>java client localhost

3

the factorial is 6

Comments

Popular posts from this blog

Add two numbers in c without using operator How to add two numbers without using the plus operator in c #include <stdio.h> int main(){         int a,b;     int sum;     printf( "Enter any two integers: " );     scanf( "%d%d" ,&a,&b);     //sum = a - (-b);     sum = a - ~b -1;     printf( "Sum of two integers: %d" ,sum);     return 0; } Sample output: Enter any two integers: 5 10 Sum of two integers: 15 Algorithm: In c ~ is 1's complement operator. This is equivalent to:   ~a = -b + 1 So, a - ~b -1 = a-(-b + 1) + 1 = a + b – 1 + 1 = a + b