gpt4 book ai didi

java - 与 wifi 和拨号互联网的 RMI 连接

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:12 25 4
gpt4 key购买 nike

我使用 rmi 创建了一个简单的数据库应用程序。它与我的本地无线网络配合良好。但现在我想通过互联网将我的客户端连接到服务器。我知道,这可以通过在路由器中设置端口转发来实现。但我希望它可以在任何使用 wifi 连接、拨号连接到互联网的计算机上工作连接等。如何做到这一点?这里要写什么? Naming.lookup("rmi://?????????????");由于我对java很陌生,请给我一个简单的代码示例的详细答案。提前致谢

最佳答案

我希望您对 Java RMI 概念感到困惑。讽刺的是,几天前我也有同样的想法,只是我想连接我的内部网络。

Java RMI 中可以使用两种类。

A Remote class is one whose instances can be used remotely. An object of such a class can be referenced in two different ways: 1. Within the address space where the object was constructed, the object is an ordinary object which can be used like any other object. 2. Within other address spaces, the object can be referenced using an object handle. While there are limitations on how one can use an object handle compared to an object, for the most part one can use object handles in the same way as an ordinary object.

A Serializable class is one whose instances can be copied from one address space to another. An instance of a Serializable class will be called a serializable object. In other words, a serializable object is one that can be marshaled.

所以,这是您问题的答案,假设您正在谈论不同系统(服务器)上的远程类(class)。

The name of a remote object includes the following information:

The Internet name (or address) of the machine that is running the Object Registry with which the remote object is being registered. If the Object Registry is running on the same machine as the one that is making the request, then the name of the machine can be omitted.

The port to which the Object Registry is listening. If the Object Registry is listening to the default port, 1099, then this does not have to be included in the name.

The local name of the remote object within the Object Registry.

远程对象的 URL 使用常用的主机、端口和名称指定:

rmi://主机:端口/名称

host = 注册表的主机名(默认为当前主机)port = 注册表端口号(默认为注册表端口号)name = 远程对象的名称

假设您的代码位于主机名为“XYZ.edu/home/CLasses”的服务器上(您可以提供服务器的 DNS/IP 地址并包含类文件的位置),port-服务器中 ABC.java 类的 number="1099"(默认)和远程对象名称="abc"。通过这种方式,人们将能够从不同的机器调用远程对象。另外,您需要将整个服务器代码保留在 Internet 地址上,以便客户端可以从 Internet 访问它们(无法访问计算机中存在的离线代码)。只有这样才会发生!!!

这是示例客户端程序:

/**
* Client program for the "Hello, world!" example.
* @param argv The command line arguments which are ignored.
*/
public static void main (String[] argv) {
try {
HelloInterface hello =
(HelloInterface) Naming.lookup ("//ortles.ccs.neu.edu/Hello"); //see here the address of the server hosting the Server file,you can omit port number,it'll take default port 1099.
System.out.println (hello.say());
} catch (Exception e) {
System.out.println ("HelloClient exception: " + e);
}
}

关于java - 与 wifi 和拨号互联网的 RMI 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com