RMI:客户端向服务器发送消息

时间:2014-09-03 18:05:01

标签: java client-server rmi

我对如何让客户端将对象发送到服务器有一个问题。 所以我有一个名为" RMIInterface"和客户" RMIClient"和服务器" RMIServer"类:

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();
        }
    }

}

使用此程序,我可以使用客户端连接到服务器,并在客户端控制台中打印一条消息。 我的问题是,如何从客户端向服务器发送消息,并在服务器控制台输出中打印该消息?请制作最简单的代码示例

3 个答案:

答案 0 :(得分:4)

虽然已经过了一年多的时间,但我会分享解决方案,以防有人来这里寻求解决问题,就像我昨天所做的那样。

<强> 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();
        }
    }
}

答案 1 :(得分:1)

只需在您的界面中添加sendMessage()方法即可。

答案 2 :(得分:0)

我不会为你提供代码,因为你已经拥有它。我将为您提供解决方案:

你已经有一个&#34;频道&#34;在哪里发送单向消息,SERVER - &gt;客户。 如果要从服务器向客户端发送消息,则客户端也必须是服务器。