简体   繁体   English

连接客户端服务器RMI

[英]connecting client-server RMI

I am confused on how the client actually makes the connection to the server if the server is remote(not on the same machine as the client). 如果服务器是远程的(与客户端不在同一台计算机上),我对客户端实际上如何建立与服务器的连接感到困惑。 My code works fine using localhost but i cant figure out how the client actually makes the connection to the server host so that it looks up the rmiregistry. 我的代码使用localhost可以正常工作,但我无法弄清楚客户端实际上是如何与服务器主机建立连接的,因此它可以查找rmiregistry。 I'm confused on what gets stored in the registry for the server, is it Sample or localhost? 我对服务器注册表中存储的内容感到困惑,是Sample还是localhost? This may be dumb but i tried to convert localhost to its ipaddress on the client side and do String url = "//" + server + ":" + 1099 + "/Sample"; 这可能是愚蠢的,但我尝试将localhost转换为客户端的ipaddress并执行String url =“ //” +服务器+“:” + 1099 +“ / Sample”; where server is the ip from getbyname() but i get a exception: java.rmi.NotBoundException: 127.0.0.1:1099/Sample That was with the client and server on both machines. 其中服务器是getbyname()的ip,但我得到一个例外:java.rmi.NotBoundException:127.0.0.1:1099/Sample这是两台计算机上的客户端和服务器。 I'm just trying to figure out how the two connect remotely but it didn't even work on the same machine using the ip address of localhost. 我只是想弄清楚两者如何远程连接,但是使用本地主机的ip地址甚至无法在同一台机器上工作。

Client: 客户:

 import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;

public class SampleClient  {
    public static void main(String args[]) {



            String url = "//" + "localhost" + ":" + 1099 + "/Sample";

            SampleInterface sample = (SampleInterface)Naming.lookup(url);



        } catch(Exception e) {
            System.out.println("SampleClient exception: " + e);
        }
    }
}

Server: 服务器:

  import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

public class SampleServer {
    public static void main(String args[]) throws IOException {

        // Create and install a security manager
        if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        try {

            String url = "//localhost:" + 1099 + "/Sample";
            System.out.println("binding " + url);
            Naming.rebind(url, new Sample());
            // Naming.rebind("Sample", new Sample());
            System.out.println("server " + url + " is running...");
        }
        catch (Exception e) {
            System.out.println("Sample server failed:" + e.getMessage());
        }
    }
}

The server should bind to a Registry running on 'localhost'. 服务器应绑定到在“ localhost”上运行的注册表。

The client should lookup a Registry at the server host. 客户端应在服务器主机上查找注册表。

It's as simple as that. 就这么简单。

I'm confused on what gets stored in the registry for the server, is it Sample or localhost? 我对服务器注册表中存储的内容感到困惑,是Sample还是localhost?

Neither. 都不是。 You're confusing three different things: 您在混淆三件事:

  1. The hostname, in this case 'localhost'. 主机名,在这种情况下为“ localhost”。
  2. The bind-name, in this case 'Sample'. 绑定名称,在这种情况下为“样本”。
  3. The object which is bound, which is the remote stub. 绑定的对象,即远程存根。

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

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