基本RMI示例

时间:2013-07-07 14:33:55

标签: java rmi

我正在尝试一个基本的RMI示例。但是每当我运行该服务时,我都会收到以下错误

java.rmi.ConnectException: Connection refused to host: 116.203.202.217; nested exception is: 
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at java.rmi.Naming.bind(Naming.java:111)
at rmi.remote.RemteImpl.main(RemteImpl.java:29)

这是代码

package rmi.remote;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RemteImpl extends UnicastRemoteObject implements RemoteIntf{

protected RemteImpl() throws RemoteException {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Override
public String sayHello() throws RemoteException {
    // TODO Auto-generated method stub
    return "hi";
}

public static void main(String a[])
{
    try {
        RemoteIntf service=new RemteImpl();
        Naming.bind("Remote",service);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}

我正在使用Windows 7操作系统

3 个答案:

答案 0 :(得分:0)

您的绑定字符串不正确。它应该是“rmi:// localhost / Remote”。您还应该检查“主机”文件,以确保它将“localhost”映射到127.0.0.1,并将您的真实主机名映射到您的真实主机地址。

答案 1 :(得分:-1)

看起来您的RMI注册表未运行,这导致绑定调用失败。您也没有绑定到URL,只是绑定到名称。

通常你会像这样调用bind:

Naming.bind("//registryHost:port/remote", service);

其中registryHost指向运行RMI注册表的计算机。

对于简单的本地测试,您可以创建URL“// localhost:port / remote”并在本地计算机上运行rmiregistry服务。

如何解释如下: http://www.javacoffeebreak.com/articles/javarmi/javarmi.html

摘录: 要启动注册表,Windows用户应执行以下操作(假设您的java \ bin目录位于当前路径中): -

启动rmiregistry 要启动注册表,Unix用户应该执行以下操作: -

rmiregistry&

答案 2 :(得分:-1)

public class RemteImpl extends UnicastRemoteObject implements RemoteIntf{
来自RemoteInf的

是一个实现Remote的接口,因此,在主代码中

  RemoteIntf service=new RemteImpl(); //avoid this

// priort添加它还考虑确保您已初始化安全管理器以允许来自或所有给定IP地址的连接。

 RemoteImpl service = new RemoteImpl();  

应改为

 RemoteInt service = new RemoteImpl();

需要服务器对象。之后,如果不想使用rmiregistry,请在代码中创建服务器注册表。

LocateRegistry.createRegistry(*port*);

最后,使用

将服务绑定到提供rmi服务的URL
String url = "rmi://127.0.0.1/RemoteObjectRegisteration"  ; //or (your remote ip 

地址127.0.0.1)

Naming.bind(url, service);

并且服务器端应该没问题。你应该花一些时间学习如何在堆栈溢出问题中提问.........