【问题标题】:RMI: Client sends a message to ServerRMI:客户端向服务器发送消息
【发布时间】:2014-09-03 18:05:00
【问题描述】:

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

RMI接口

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

}

RMI服务器

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

}

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

【问题讨论】:

    标签: java client-server rmi


    【解决方案1】:

    虽然已经一年多了,但我分享解决方案,以防有人像我昨天一样来这里寻求解决问题。

    RMI接口

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

    RMI服务器

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 好的,但是如何在该方法中连接到服务器并将对象发送到服务器?我应该在 sendMessage() 中写什么代码?
      • 能否请您给我一个代码,然后我可以接受您的回答。谢谢!
      • 放下它。我已经告诉过你该怎么做。我希望任何称职的程序员都能将其转化为方法声明。
      【解决方案3】:

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

      您已经有一个“通道”,您可以在其中发送单向消息,SERVER -> CLIENT。 如果你想从你的服务器向你的客户端发送消息,你的 CLIENT 也必须是一个 SERVER。

      【讨论】:

      • 他想从客户端向服务器发送消息。他已经从服务器向客户端发送了一条消息,这反驳了你的最后一句话。
      猜你喜欢
      • 2015-01-05
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 2017-07-30
      相关资源
      最近更新 更多