Java Rmi UnknownHostException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13370912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:28:15  来源:igfitidea点击:

Java Rmi UnknownHostException

javarmi

提问by user1822343

I am trying to write a simple RMI file but for some reason it is giving me an unknown host nameexception. I have tried simply everything before actually asking a question. Here is the code for the project.

我正在尝试编写一个简单的 RMI 文件,但由于某种原因,它给了我一个未知的主机名异常。在实际提问之前,我已经尝试了一切。这是该项目的代码。

RMI interface class:

RMI接口类:

import java.rmi.Remote;
import java.rmi.RemoteException;

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

Server Class:

服务器类:

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

public class Server implements Hello {

    public Server() {}

    public String sayHello() {
        return "Hello, world!";
    }

    public static void main(String args[]) {

        try {
            Server obj = new Server();
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

            // Bind the remote object's stub in the registry
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Hello", stub);

            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

Client class:

客户端类:

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

public class Client {

    private Client() {}

    public static void main(String[] args) {

        String host = (args.length < 1) ? null : args[0];
        try {
            Registry registry = LocateRegistry.getRegistry(host);
            Hello stub = (Hello) registry.lookup("Hello");
            String response = stub.sayHello();
            System.out.println("response: " + response);
        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

When I run this over localhost this runs completely fine but for the life of me I cannot get this to run over the network across two machines. I've tried:

当我在本地主机上运行它时,它运行完全正常,但对于我的生活,我无法让它在两台机器上的网络上运行。我试过了:

  • Connecting the machines over Wifi.
  • Connecting over ethernet so they have addresses like 192.168.1.1and 192.168.1.2respectively. Both machines can ping each other fine. If I nma -PNthe server from the client I can see that port 1099is open and the rmiregistryis running. But not matter what combination of hostnames I try it still doesn't connect the client to the server.
  • 通过 Wifi 连接机器。
  • 通过以太网连接,因此它们分别具有类似192.168.1.1和 的地址192.168.1.2。两台机器可以互相ping通。如果我nma -PN从客户端访问服务器,我可以看到该端口1099已打开并且rmiregistry正在运行。但是无论我尝试什么主机名组合,它仍然无法将客户端连接到服务器。

Combinations I've tried for input as host name on client side:

我尝试在客户端作为主机名输入的组合:

rmi://192.168.1.1
rmi://192.168.1.1:1099

And maybe tens more with all possible slashes etc. still no clue why this isn't working (even though it works perfectly over localhost). Any ideas would be welcome.

并且可能还有数十个带有所有可能的斜杠等。仍然不知道为什么这不起作用(即使它在本地主机上运行良好)。欢迎任何想法。

For completeness here is the actualy error I get:

为了完整起见,这是我得到的实际错误:

 java -classpath classes/ Client
Client exception: java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
    java.net.UnknownHostException: rmi://192.168.1.1
java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
    java.net.UnknownHostException: rmi://192.168.1.1
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:598)
    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.lookup(Unknown Source)
    at Client.main(Client.java:14)
Caused by: java.net.UnknownHostException: rmi://192.168.1.1
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:189)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
    ... 5 more

回答by user1822343

This was resolved. I'll just post my own solution. Apparently running the server like this works: java -Djava.rmi.server.hostname=192.168.1.1 Server.The problem was that when the client calls the server and gets back a stub, this stub contains this property and java sets this property to localhost by default, even if you bind the server to an ip when creating a registry. That is why it seems even though your client is connecting to a remote host, it crashes saying connection refused on localhost because the stub it has contains localhost.

这已解决。我只会发布我自己的解决方案。显然,像这样运行服务器是有效的:java -Djava.rmi.server.hostname=192.168.1.1 Server.The 问题是当客户端调用服务器并取回一个存根时,这个存根包含这个属性,java 将此属性设置为默认情况下为 localhost,即使您在创建注册表时将服务器绑定到 ip。这就是为什么即使您的客户端连接到远程主机,它也会崩溃,说 localhost 上的连接被拒绝,因为它的存根包含 localhost。

回答by George

Just a comment on the mapping between host name and IP: you can name you locator whatever you like but then you will have to insert an entry in your hosts file (C:\Windows\system32...\hosts), e.g.

只是对主机名和 IP 之间的映射的评论:您可以随意命名定位器,但随后您必须在主机文件(C:\Windows\system32...\hosts)中插入一个条目,例如

127.0.0.1  rmihost1

Then you can rmihost1 in the call:

然后你可以在调用 rmihost1 :

LocateRegistry.getRegistry(host, port);