Click here to Skip to main content
15,884,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new in Java RMI and trying to do some learning in different condition. I don't know what I am trying to do is appropriate or not.  I am trying to set a variable value during starting the server which is accessible to client. Let me explain what I have tried so far and what I am trying to do.


What I have tried:

So far I have developed an RMI application where client gives an input & get the factorial for that. I have 4 class for the application.
Those along with code are following.

Server class - FacServer
Java


import java.rmi.*;
public class FacServer.java
{
public static void main(String a[]) throws Exception
{
    FacImpl obj = new FacImpl();
    Naming.rebind("FAC",obj);
    System.out.println("Server started");
}
}

Client Class - FacClient.java
Java
import java.util.*;
import java.io.*;
import java.rmi.*;

public class FacClient
{
public static void main(String a[]) throws Exception
{
  FacInt obj = (FacInt)Naming.lookup("FAC");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter a number: ");
  String s = null;
  try {
     s = br.readLine();
  } catch(IOException ioe) {
     System.err.println(ioe.getMessage());
  }
    int r = Integer.parseInt(s);
    int n= obj.fac(r);

    System.out.println("Factorial is "+n);
}
}

Implementation Class - FacImpl.java
Java
<pre>import java.rmi.server.*;
public class FacImpl extends UnicastRemoteObject implements FacInt 
{
public FacImpl() throws Exception
{
    super();
}
public int fac(int x)
{
int i,j=1;
for(i=x;i>1;i--)
j=j*i;
return j;
}
}

Interface Class - FacInt.java
Java
import java.rmi.Remote;
public interface FacInt extends Remote
{
public int fac(int x) throws Exception;
}

It works fine. Now I want to set some limitation in server side. For example server won't calculate the factorial value for more than 5. I know I can do it in implementation class easily. But I want to declare it when I start the server. For example to set limitation 5 I should start the server in following way.

java FacServer 5

So if client input a value more than 5 it will get an error reply. Is it possible to do so?

Also can I show the factorial value in server side also if client provide an accepted value?
Posted
Updated 4-Nov-17 4:28am

You can modify your server and implementation as follows:
Java
public class FacServer.java
{
public static void main(String a[]) throws Exception
{
    int maximum = 5; // default max value
    if (a.length > 0)
        maximum = Integer.parseInt(a[0]);
    FacImpl obj = new FacImpl(maximum);
    Naming.rebind("FAC",obj);
    System.out.println("Server started");
}
}

Java
<pre>public class FacImpl extends UnicastRemoteObject implements FacInt 
{
    int maxValue;
    public FacImpl(int max) throws Exception
    {
        super();
        maxValue = max;
    }

    public int fac(int x)
    {
        if (x > maxValue)
        {
             // error code here
        }
        int i,j=1;
        for(i=x;i>1;i--)
        j=j*i;
        return j;
    }
}
 
Share this answer
 
Comments
Member 13441993 4-Nov-17 17:02pm    
Thank you so much. It's working well. Can I show the factorial value in server side also?
Thanks a lot. That worked like a charm.

I have edited the code in following way.

FacImpl
Java
import java.rmi.server.*;
import java.util.*;
import http://java.io.*;
import java.math.*;
public class FacImpl extends UnicastRemoteObject implements FacInt
{
int limit;
public FacImpl(int limit) throws Exception
{
super();
this.limit = limit;
}
public int fac(int x) throws Exception
{
if (x > limit)
throw new IllegalArgumentException(
String.format("Cannot calulate Fac beyond %1", limit));
else{
int i,j=1;
for(i=x;i>1;i--)
j=j*i;
return j;
}
}
}

FacServer
Java
import java.rmi.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class FacServer
{
public static void main(String a[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
String s = null;
try {
s = br.readLine();
} catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
int limit = Integer.parseInt(s);
if ((a != null) && (a.length > 0)) {
try {
int temp = Integer.parseInt(a[0]);
if (temp > 0)
limit = temp;
} catch (NumberFormatException e) {
}
}
FacImpl obj = new FacImpl(limit);
Naming.rebind("FAC",obj);
System.out.println("Server started");
}
}

Also I wanted to show the factorial result in server instead of client. Is it possible to do so?
 
Share this answer
 
Comments
Richard MacCutchan 5-Nov-17 3:00am    
If the server has a connection to a screen or window then you should be able to use System.out just the same as at the client.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900