Java 基本RMI示例

Java 基本RMI示例,java,rmi,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.tra

我正在尝试一个基本的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操作系统,看起来您的RMI注册表没有运行,这导致绑定调用失败。您也没有绑定到URL,只是绑定到名称

通常您会这样称呼bind:

Naming.bind("//registryHost:port/remote", service);
其中registryHost指向运行RMI注册表的计算机

对于一个简单的本地测试,您需要创建URL“//localhost:port/remote”,并在本地机器上运行rmiregistry服务

下面举例说明如何执行此操作:

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

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

注册处&

public class RemteImpl extends UnicastRemoteObject implements RemoteIntf{
from RemoteInf是一个接口,它在主代码中实现远程

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

/PrimoTo添加这个也考虑确保您已经初始化了允许从所有IP地址连接的安全管理器。p>

 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)


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

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

我想,这比连接超时要清楚得多。。。。。您确定另一方正在侦听并且可以访问,并且您的防火墙没有阻止您吗?似乎端口正在使用,或者您有防火墙issues@user2511414连接超时不是由端口使用中的问题引起的。这里没有证据表明注册表没有运行,除了你无数的打字错误之外,你建议的改变前后没有区别-1这将导致“连接被拒绝”,而不是连接超时。您只能绑定到本地主机中运行的注册表,因此在绑定、重新绑定或解除绑定时,除了指定“本地主机”之外,没有任何其他指定。
Naming.bind(url, service);