0

I have developed a java rmi program as shown below now the only thing that i want to add in it is that soon when the client send the request to server , server should capture the client details that is the client details like ip , please advise how can i add listeners so that the moment client send the request the server should capture the details ..

below is my program ..

interface :-

import java.rmi.*;
public interface AddServerInterface extends Remote {
    public int sum(int a,int b);
}

implementation class :-

import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject implements AddServerInterface {
    Adder()throws RemoteException{
        super();
    }

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

RMI service :-

import java.rmi.*;
import java.rmi.registry.*;
public class AddServer {

    public static void main(String args[]) {
        try{
            AddServerInterface addService=new Adder();
            Naming.rebind("AddService",addService); 
            //addService object is hosted with name AddService. 

        } catch(Exception e){System.out.println(e);}
    }
}

client application :-

import java.rmi.*;
public class Client {
    public static void main(String args[]) {
        try{
            AddServerInterface st=(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/AddService");
            System.out.println(st.sum(25,8));
        } catch(Exception e){System.out.println(e);}
    }
}

please advise how can i add the functionality of passing client info the server

2 Answers2

1

server should capture the client details that is the client details like IP

The client's IP address is available during a remote method invocation via RemoteServer.getClientHost(). If you want other details you will just have to tell us what they are.

please advise how can i add listeners so that the moment client send the request the server should capture the details

As far as the client's IP address is concerned, you don't need a listener. You probably don't need a listener for any of whatever you're trying to do: see below.

This and your prior question smell strongly of an XY problem. What exactly are you trying to accomplish? It all sounds to me like a case for the Remote Session Pattern, but until you confide further it is impossible to be sure.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
0

If you want to pass additional info apart from IP address of the client to server, you can expand sum method in remote interface and pass that information as additional parameters.

IP info from the client is not required for RMI server as RemoteServer.getClientHost() at RMI remote server end returns the IP of the client which made the remote call.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211