gpt4 book ai didi

java - 在 RMI 期间知道端口号

转载 作者:搜寻专家 更新时间:2023-11-01 02:52:33 24 4
gpt4 key购买 nike

有没有办法在RMI时同时知道客户端和服务端的端口号?

当RMI中返回一个结果给客户端,下次客户端请求结果时,客户端和服务器之间的对话是否会和上一次在同一个端口,当服务器从第一个结果返回后一直在运行还是创建了一个新端口?

例如:

// call to a remote method add
addServerIntf.add(d1,d2)
// after this call i get the added result

// again call the add method by again starting the client
addServerIntf.add(d1,d2)
// I get the result back as usual

在两次不同的调用中,客户端和服务器的端口号是否保持不变?

我的客户端程序在输入类似 java AddClient localhost 100 200 命令后退出,服务器上的方法返回300,客户端退出。下次我再次使用 java AddClient localhost 19 100 启动我的客户端时。现在将从与之前发送的相同端口发送请求,服务器是否会在相同端口上接收请求?或者情况与我刚刚写的不同?

最佳答案

Is there a method to know the port number of both the client and server during RMI?

如果我没有误解的话,我认为在 RMI session 期间没有办法知道端口号。

如果你有一个不同的端口号,从默认的 RMI 注册表服务器 1099 端口,你必须在服务器类和客户端类上设置它,因为,作为 Oracle RMI page报告:

If the registry will be running on a port other than 1099, you'll need to specify the port number in the calls to LocateRegistry.getRegistry in the Server and Client classes. For example, if the registry is running on port 2001 in this example, the call to getRegistry in the server would be:

Registry registry = LocateRegistry.getRegistry(2001);

和:

This client first obtains the stub for the registry by invoking the static LocateRegistry.getRegistry method with the hostname specified on the command line. If no hostname is specified, then null is used as the hostname indicating that the local host address should be used.

然后,我的结论是,你无法通过一种方法知道 RMI session 的端口号(你可以查看 RMI API 了解详细信息),除非你必须设置它,如果它不同于默认的 RMI 注册表服务器 1099 端口,因为您必须在 RMI session 开始时知道它。

试想:如何获得这个端口号?联系服务器还是客户端?例如,如果您请求位于特定服务器中的页面,该页面正在监听端口 81(而不是默认的 80 端口),您需要提前连接到该特定服务器的端口号,联系它,例如: http://192.168.1.1:81。然后,在 RMI session 期间,您必须提前知道 RMI 注册端口。

检查这个page了解详情。

When a result is returned during RMI to the client, next time the client requests the result, will the conversation between client and server be at the same port as the previous, when the server has been running since the first result was returned or a new port is created?

当结果返回给客户端时,客户端和服务器之间的对话应该在同一个RMI注册端口上共享,否则,如果客户端中设置的RMI注册端口与服务器RMI注册端口不同(如果我没有忘记),代码将抛出一个 RemoteException,当 RMI 进程发生故障时可能会发生这种情况。

更新

现在我看到了你更新的问题。

In the 2 different calls does the port number of client and server remain the same ?

它应该是相同的 RMI 注册表服务器端口。当您的程序在第一次调用后退出时,程序将关闭套接字连接。下次启动程序时,RMI 注册端口应该是相同的。否则,您的程序应该抛出异常,或者,当您将参数传递给程序时,您会得到意想不到的结果。如果我理解的话,您的客户端程序只是调用服务器上的 sum 方法。在第一个结果之后,下次启动程序时,会得到不同的结果吗?如果不是,我认为 RMI 注册表端口是相同的。

关于java - 在 RMI 期间知道端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8591990/

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