RMI与不同的PC

时间:2016-05-07 16:11:09

标签: java rmi

我正在学习RMI,我制作了一个使用代码库的基本程序。

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

public class Server implements sInterface,s2int  {

public void go()
{
    System.out.println("GO");
}
public void doIt()
{

}

public static void main(String[] args)
{
    if(System.getSecurityManager()==null)
    {
        System.setSecurityManager(new SecurityManager());
    }
    try
    {
    System.setProperty("java.rmi.server.hostname","helios");
        String s = "SERVER";
        Registry r = LocateRegistry.getRegistry();

        sInterface stub = (sInterface) UnicastRemoteObject.exportObject(new Server(),0);
        r.rebind(s,stub);

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

客户端:

public class Client {

public static void main(String[] args)
{
    if(System.getSecurityManager()==null)
    {
        System.setSecurityManager(new SecurityManager());
    }

    try{

        String name = "SERVER";
        Registry r = LocateRegistry.getRegistry(args[0]);
        sInterface inf = (sInterface)r.lookup(name);
        inf.go();

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

客户端没有's2int'接口,并且从代码库下载。 用于启动服务器和客户端模块的命令如下:

java -Djava.rmi.server.codebase=http://helios/~owner/rmi.jar
-Djava.security.policy=server.policy Server         


java -Djava.security.policy=client.policy    
-Djava.rmi.server.codebase=http://helios/~owner/     
-Djava.rmi.server.hostname=helios Client localhost

现在,当服务器和客户端都在同一台PC上时,这是有效的,但是当我尝试在同一网络上的另一台PC上运行它时,我得到了一个

java.rmi.ConnectException: Connection refused to host: localhost; nested     exception is:
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
    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 Client.main(Client.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at          
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource) at    
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
    ... 6 more

在网络方面我不是很了解。任何人都可以解释为什么它不起作用?

1 个答案:

答案 0 :(得分:1)

您的客户正在查找错误的注册表。它需要在服务器主机上查找注册表,而不是它自己的localhost。