有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVArmi。ConnectionException:远程端点处的非JRMP服务器

这是一个简单的RMI程序,但是,当我运行HelloClient时,它总是抛出异常。爪哇

创建远程接口

public interface Hello extends Remote {
    String sayHello(String name) throws RemoteException;
}

创建远程类

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {

    protected HelloImpl() throws RemoteException {
        super();
    }

    @Override
    public String sayHello(String name) throws RemoteException {
        System.out.println("HelloImpl:" + name);
        return name;
    }
}

创建服务器:

import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class HelloServer {
    public static final int port = 1099;

    public static void main(String[] args) {
        try {
            if (System.getSecurityManager() == null) {
                System.setSecurityManager(new RMISecurityManager());
            }
            Registry registry = LocateRegistry.createRegistry(port);
            HelloImpl impl = new HelloImpl();
            registry.rebind("//SEJ1T1DYN68BZBF:1099/HelloService", impl);
            String[] names = registry.list();
            for (String name : names) {
                System.out.println(name);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

创建客户端:

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class HelloClient {

    public static void main(String[] args) {
        try {
            Registry registry = LocateRegistry.getRegistry();
            Hello hello = (Hello) registry
                    .lookup("//SEJ1T1DYN68BZBF:1099/HelloService");
            System.out.println(hello.sayHello("javamaj blog"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

例外情况是:

java.rmi.ConnectIOException: non-JRMP server at remote endpoint
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at HelloClient.main(HelloClient.java:10)

环境: jdk 1.7+eclipse+Windows xp


共 (1) 个答案

  1. # 1 楼答案

    在客户端主机的端口1099上运行的不是RMI注册表

    但是,除非客户机主机和服务器主机是同一台主机,否则无论如何,您查找的注册表都是错误的。您需要使用服务器主机名调用getRegistry(),以便在服务器主机上查找注册表:服务器绑定到的主机