clie*nt_Jav*_rmi

i have implement a simple clie*nt,serv*er,inter*face in java rmi and i want to modify so a client
to sent to server except from string which i want to represent a variable and the value of this variable.
(I am new to java rmi programming and i have complete confused)

client

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient
{

static public void main(String args[]) throws Exception
      {
              //System.setSecurityManager(new RMISecurityManager());
            ReceiveMessageInterface rmiServer;
            Registry registry;
            String serverAddress=args[0];
            String serverPort=args[1];
            String text=args[2];
            System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
      
      try{
            // get the registry 
            registry=LocateRegistry.getRegistry(
            serverAddress,(new Integer(serverPort)).intValue());

            // look up the remote object
            rmiServer=(ReceiveMessageInterface)(registry.lookup ("rmiServer"));

            // call the remote method
            rmiServer.receiveMessage(text);

      }

      catch(RemoteException e){e.printStackTrace();}
      catch(NotBoundException e){e.printStackTrace();}
      
}
}

server

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.

// This method is called from the remote client by the RMI.
// This is the implementation of the ReceiveMessageInterface.
public void receiveMessage(String x) throws RemoteException
{
      System.out.println(x);
      System.out.println(y);

}


public RmiServer() throws RemoteException
{
      try{
      // get the address of this host.
      thisAddress= (InetAddress.getLocalHost()).toString();
      }
      catch(Exception e){throw new RemoteException("can't get inet address.");}

      thisPort=3231; // this port(registrys port)
      System.out.println("this address="+thisAddress+",port="+thisPort);
      try{
      // create the registry and bind the name and object.
      registry = LocateRegistry.createRegistry( thisPort );
      registry.rebind("rmiServer", this);
      } catch(RemoteException e){throw e;}
}

static public void main(String args[])
{
      try{
      RmiServer s=new RmiServer();
      }
      catch (Exception e) {e.printStackTrace();System.exit(1);}
}

}


interface

import java.rmi.*;

public interface ReceiveMessageInterface extends Remote

{

      void receiveMessage(String x,int y) throws RemoteException;

}






Tom211Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rbrindlCommented:
The code looks ok, what is the problem? Does it work now?
Do I understand right, that you want to send data different from String?
Just change the interface! The type just has to be Serializable (i.e. the runtime type).
0
Tom211Author Commented:
actually i had a mistake on the server's code but i fix it now.
i just do as a comment the
      System.out.println(y);

How i can do:
server to add 1 to the client's value and sending back to the client   ???????
0
Tom211Author Commented:
No respond yet????
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

rbrindlCommented:
on the server you add a function "add" and let it return the value, as in:

public int add(final int a , final int b)
{
    return a+b;
}

You can find that in every textbook on RMI as one on the first examples.

In case you meant asynchronously returning the value:
RMI cant do that by itself.
You would create some kind of threadpool at the server that does the processing.
When the results are finished, it would than, for example to a callback to the original caller. Essentially this would be a RMI connection in the other direction (You would send a callback object via call by reference semantics, which basically involves a dynamic proxy that handles the callback).
I would not use RMI for asynchronous communication, because it is basically a synchronous protocol. Message based communication is much better suited for this (e.g. JMS).
0
Tom211Author Commented:
I try this change to the server but create the following problem :
final parameter may not be assigned

the new source of server is :


import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;

public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.

// This method is called from the remote client by the RMI.
// This is the implementation of the ReceiveMessageInterface.
public void receiveMessage(String x) throws RemoteException
{
      System.out.println(x);


}


public RmiServer() throws RemoteException
{
      try{
      // get the address of this host.
      thisAddress= (InetAddress.getLocalHost()).toString();
      }
      catch(Exception e){throw new RemoteException("can't get inet address.");}

      thisPort=3231; // this port(registrys port)
      System.out.println("this address="+thisAddress+",port="+thisPort);
      try{
      // create the registry and bind the name and object.
      registry = LocateRegistry.createRegistry( thisPort );
      registry.rebind("rmiServer", this);
      } catch(RemoteException e){throw e;}
}

public int add(final int a , final int x)
{
return a=x+1;
}

static public void main(String args[])
{
      try{
     RmiServer s=new RmiServer();
      }
     
      catch (Exception e) {e.printStackTrace();System.exit(1);}
}

}
0
Tom211Author Commented:
No respond yet???
0
rbrindlCommented:
ah, sorry, i've already written the reply, then my machine crashed and i forgot:

ok, the problem is:

public int add(final int a , final int x)
{
   return a=x+1;
}

what you are doing here is to assign the value to one of the parameters, which obviously is not a good idea, since the parameters are final in this case.

if you really want to add only one, you would do:

public int inc(final int a )
{
   return a+1;
}

what i meant was:

public int add(final int a , final int x)
{
   return a + x;
}

i.e. not just increment by 1, but supply a generic add method, that is capable of adding whatever amount x you choose to the given base a.
0
Tom211Author Commented:
I did the change and i think that know is ok this function.But i found a new  problem in client this time at
            rmiServer.receiveMessage(text);

This is because at first (before i modified) the client was sent text.How to modify to sent int values?????
0
rbrindlCommented:
at the client you now have:

            // call the remote method
            rmiServer.receiveMessage(text);

If you do a

    int x = rmiServer.inc(10);
    System.out.println("The value of x: "+x);

This should output 11

0
Tom211Author Commented:
OK. If  want to give two strings variables  with one int variable, for each one.
For example i want to give
value1 2
value2 3
and the server return to client  
value1 3
value2 4

 How to do this???
0
rbrindlCommented:
if you want this to return a String, you would do:

public String labelledInc(final String label, final int value )
{
   return label+" "+(value+1);
}

if you want to have both label and value returned, (although that has no sense in this case, because the label is not processed in any way remotely, so why do you send it anyway?), then you would need to create a value object, that holds both label and value,  and submit and return that (direct property access for clarity)::

public class LabelledValue {
   String label;
   int value;
}

public LabelledValue labelledInc(final LabelledValue val )
{
   val.value++;
   return val;
}

With RMI there is basically no call by reference, so you need to return the value, and can not modify it as side effect.
Call by reference would require to work with proxies, that provide the remoting, but i guess that is far beyond the scope of this question.

I would also suggest that you do a google search for "java rmi introduction".
There are tons of examples and introductions around that handle exactly those questions.
0
Tom211Author Commented:
And how can i call this function from the client .
0
rbrindlCommented:
LabelledValue v= new LabelledValue()
v.val=10;
v.label="Lightyears";
LabelledValue result = rmiServer.labelledInc(v);

just as you did with rmiServer.receiveMessage(text); before.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom211Author Commented:
i try to implement this class

public int aaa(final int a,final int x)throws RemoteException
       {if (x>2){
       
      static int x=2;
      System.out.println("is:"+y);
      System.out.println(" is:"+x);
        return a-(3*x);
               }
      }

as you can see i try to check the parameter x.If x is bigger than 2 i want to be 2 (calculate to be 2)


how can i do this?
 
0
Tom211Author Commented:
no response yet???
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.