简体   繁体   English

有没有办法确定远程RMI Server计算机是否是多宿主的,即使它的某些IP地址不可见?

[英]Is there a way to determine if a remote RMI Server machine is multi-homed, even if some of its IP addresses are not visible?

I'm running into a couple of snags getting RMI Servers to work properly on multi-homed machines. 我遇到了一些麻烦,使RMI服务器在多宿主计算机上正常工作。 I've written a custom RMISocketFactory and I've tried implementing the client-side createSocket(String host, int port) method a couple of different ways. 我已经编写了一个自定义RMISocketFactory并尝试了几种不同的方法来实现客户端的createSocket(String host, int port)方法。 One way only works when the RMI server machine IS multi-homed, the other way only works when the RMI server machine is not. 一种方法仅在RMI服务器计算机为多宿主时有效,另一种方法仅在RMI服务器计算机为非宿主时有效。 Here is my code for both createSocket method versions in my client-side RMISocketFactory implementation: 这是我的客户端RMISocketFactory实现中两个createSocket方法版本的代码:

Works only when the RMI Server IS NOT Multi-homed: 仅当RMI服务器不是多宿主时才有效:

@Override
public Socket createSocket(String host, int port) throws IOException{
    Socket s = new Socket(host,port);

    //Do some book-keeping here ...

    return s;
}

Works only when the RMI Server IS Multi-homed: 仅当RMI服务器为多宿主时才有效:

private TreeSet<String> validHosts = null;

@Override
public Socket createSocket(String host, int port) throws IOException{
    Socket s = null;

    try {
        s = new Socket(host,port);
        synchronized(this){
            if(validHosts == null) validHosts = new TreeSet<String>();
            validHosts.add(host);
        }
    } catch (IOException e) {}

    if(s == null && validHosts != null){
        synchronized(this){
            for(String h : validHosts){
                try {
                    s = new Socket(h,port);
                } catch (IOException e) {}
            }
        }
    }

    if(s == null){
        try {
            String h = RemoteServer.getClientHost();
            s = new Socket(RemoteServer.getClientHost(),port);

            synchronized(this){
                if(validHosts == null) validHosts = new TreeSet<String>();
                validHosts.add(h);
            }
        } catch (Exception e) {}
    }

    //No dice - throw an exception:
    if(s == null){
        TreeSet<String> set = new TreeSet<String>();
        set.add(host+"(orig)");

        synchronized(this){
            if(validHosts != null) set.addAll(validHosts);
        }

        StringBuilder sb = new StringBuilder(
            "Could not connect socket on port "+port+
            " to any of the following hosts: ");
        for(String h : set) sb.append(h+" ");

        throw new IOException(sb.toString());
    }

    //Do some book-keeping here ...

    return s;
}

Question

It would seem that if I could somehow tell if the server-side machine was multi-homed, then I could just wrap both sets of code in an if(serverIsMultihomed) ... else ... kind of statement. 看来,如果我能以某种方式判断服务器端机器是否是多宿主的,那么我可以将两组代码都包装在if(serverIsMultihomed) ... else ...这样的语句中。 I do have the ability to get the actual machine name of the server within this call, but calling InetAddress.getAllByHostName(host) doesn't actually return all of the host's IP addresses, only ones that are visible to the NIC on the client's machine. 我确实能够在此调用中获取服务器的实际计算机名称,但是调用InetAddress.getAllByHostName(host)并不会真正返回所有主机的IP地址,仅返回客户端计算机上NIC可见的那些IP地址。 。 So when createSocket(host,port) gets called with an IP address that isn't actually visible to the client, this is where I run into issues. 因此,当使用客户实际上不可见的IP地址调用createSocket(host,port)时,这就是我遇到的问题。 Furthermore, setting the java.server.rmi.hostname to the IP address of the server that I want to use doesn't fix this problem, and would essentially forbid machines on different subnets that the server is connected to from establishing a link. 此外,将java.server.rmi.hostname设置为我要使用的服务器的IP地址并不能解决此问题,并且实际上将禁止该服务器连接到的不同子网上的计算机建立链接。

The short of it is, is there another way to get all of a host's IP addresses, visible or otherwise, from a different machine, or am I totally barking up the wrong tree here? 简而言之,是否有另一种方法可以从另一台机器上获得主机的所有IP地址(可见或不可见),或者我是否完全在这里树错了树?

您是否看过此多宿主解决方案

If the server has one IP address that's visible to everybody, which seems to be the only multihoming case that Sun considered, all you need to does set java.rmi.server.codebase to that address at the server JVM. 如果服务器具有一个所有人都可以看到的IP地址,这似乎是Sun所考虑的唯一一种多宿主情况,那么您需要做的就是将java.rmi.server.codebase设置为服务器JVM上的该地址。 No socket factories required at all. 完全不需要插座工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM