0

我正在尝试一个基本的 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 操作系统

4

3 回答 3

0

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

于 2013-07-08T01:55:54.370 回答
-1
public class RemteImpl extends UnicastRemoteObject implements RemoteIntf{

from RemoteInf is a interface which implements Remote, so, in your main code

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

//priort to adding this also consider to make sure you have initialized security Manager for allowing connection from or all given IP address.

 RemoteImpl service = new RemoteImpl();  

should be changed to

 RemoteInt service = new RemoteImpl();

where a server object is needed. After that , create a server registry in your code if don't want to use rmiregistry.

LocateRegistry.createRegistry(*port*);

finally, bind the service to a url where a rmi service is provided, using

String url = "rmi://127.0.0.1/RemoteObjectRegisteration"  ; //or (your remote ip 

address on place of 127.0.0.1)

Naming.bind(url, service);

and the server side should be okay. and You should take some time learning how to ask questions in stack overflow.........

于 2013-07-07T14:50:58.057 回答
-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 用户应该执行以下操作:-

rmir​​egistry &

于 2013-07-07T14:42:48.553 回答