Alex230
Alex230

Reputation: 3

RMI: Client sends a message to Server

I have a problem understanding, on how to make my client to send the object to a server. So I have one interface called "RMIInterface" and client "RMIClient" and server "RMIServer" classes:

RMIInterface

public interface RMIInterface extends Remote {

   public String getMessage(String text) throws RemoteException;

}

RMIClient

public class RMIClient {

    private void connectToServer() {
        try {
            Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
            RMI rmi = (RMI) registry.lookup("server");
            System.out.println("Connected to Server");

            String text = rmi.getMessage("RMITest Message");
            System.out.println(text);
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public static void main(String[] args) {
        RMIClient client = new RMIClient();
        client.connectToServer();
    }

}

RMIServer

public class RMIServer extends UnicastRemoteObject implements RMIInterface {

    public RMIServer() throws RemoteException {
        super();
    }

    @Override
    public String getMessage(String text) throws RemoteException {
        return "Your message is: " + text;
    }

    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.rebind("server", new RMIServer());
            System.out.println("Server started!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

With this program I can connect to a Server with a Client, and print in Client console a message. My quesiton is, how can I send message from a client to a server, and print that message in server console output? Please make the simplest code example

Upvotes: 0

Views: 6675

Answers (3)

igarciadev
igarciadev

Reputation: 131

Although it has been over a year, I share solution in case someone comes here looking to solve the question as I did yesterday.

RMIInterface

import java.rmi.*;

public interface RMIInterface extends Remote {

    public void sendMessage(String text) throws RemoteException;

    public String getMessage(String text) throws RemoteException; 

}

RMIClient

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

public class RMIClient {

    public static void main(String[] args) {

        String text = "RMI Test Message";
        RMIInterface rmi = null;

        try {
            Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
            rmi = (RMIInterface) registry.lookup("server");
            System.out.println("Connected to Server");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (rmi != null) {
            try {
                rmi.sendMessage(text);
                System.out.println(rmi.getMessage(text));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            System.out.println("Finished");
        }
    }
}

RMIServer

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

public class RMIServer implements RMIInterface {

    @Override
    public void sendMessage(String s) throws RemoteException {
        System.out.println(s);
    }

    @Override
    public String getMessage(String text) throws RemoteException {
        return "Your message is: " + text;
    }

    public static void main(String[] args) {
        Registry reg = null;
        try {
            reg = LocateRegistry.createRegistry(1099);
        } catch (Exception e) {
            System.out.println("ERROR: Could not create the registry.");
            e.printStackTrace();
        }
        RMIServer serverObject = new RMIServer();
        System.out.println("Waiting...");
        try {
            reg.rebind("server", (RMIInterface) UnicastRemoteObject.exportObject(serverObject, 0));
        } catch (Exception e) {
            System.out.println("ERROR: Failed to register the server object.");
            e.printStackTrace();
        }
    }
}

Upvotes: 7

user207421
user207421

Reputation: 311054

Just add a sendMessage() method to your interface.

Upvotes: 1

AndreDuarte
AndreDuarte

Reputation: 804

I'm not gonna provide you the code, cause you already have it. I'm gonna provide you with a solution:

You already have a "channel" where you send a unidirectional message, SERVER -> CLIENT. If you want to send a message from you server to your client, your CLIENT must be a SERVER too.

Upvotes: 0

Related Questions