如何确保RMI仅使用一组特定的端口? [英] How do I ensure that RMI uses only a specific set of ports?

查看:116
本文介绍了如何确保RMI仅使用一组特定的端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们以非常不同的方式使用RMI进行客户端 - 服务器通信:

In our application, we are using RMI for client-server communication in very different ways:


  1. 将数据从服务器推送到要显示的客户端。

  2. 从客户端向服务器发送控制信息。

  3. 从服务器返回的那些控制消息的回调代码路径对于客户端(侧栏说明 - 这是一些遗留代码的副作用,而不是我们的长期意图)。

我们想要做的是确保我们所有与RMI相关的代码仅使用已知的指定端口库存。这包括注册表端口(通常预期为1099),服务器端口以及回调产生的任何端口。

What we would like to do is ensure that all of our RMI-related code will use only a known specified inventory of ports. This includes the registry port (commonly expected to be 1099), the server port and any ports resulting from the callbacks.

这是我们已经知道的:


  1. LocateRegistry.getRegistry(1099)或Locate.createRegistry(1099)将确保注册表正在监听1099.

  2. 使用带有端口参数的UnicastRemoteObject构造函数/ exportObject静态方法将指定服务器端口。

这些点也包含在这个 Sun论坛帖子

These points are also covered in this Sun forum post.

我们不知道的是:我们如何确保回调产生的客户端连接回服务器只能连接到指定的端口,而不是默认为一个匿名端口?

What we don't know is: how do we ensure that the client connections back to the server resulting from the callbacks will only connect on a specified port rather than defaulting to an anonymous port?

编辑:添加了一个很长的答案,总结了我的发现以及我们如何解决问题。希望这可以帮助其他有类似问题的人。

Added a longish answer summarizing my findings and how we solved the problem. Hopefully, this will help anyone else with similar issues.

第二次编辑:事实证明,在我的应用程序中,我的创建和修改似乎存在竞争条件插座工厂。我本来想让用户在Beanshell脚本中覆盖我的默认设置。遗憾的是,在工厂创建第一个套接字后,我的脚本似乎正在运行。因此,我从默认设置和用户设置中获得了混合端口。将需要做更多的工作,这超出了这个问题的范围,但我想我会指出它是其他可能不得不在某些时候踏上这些水域的人的兴趣点....

SECOND It turns out that in my application, there seems to be a race condition in my creation and modification of socket factories. I had wanted to allow the user to override my default settings in a Beanshell script. Sadly, it appears that my script is being run significantly after the first socket is created by the factory. As a result, I'm getting a mix of ports from the set of defaults and the user settings. More work will be required that's out of the scope of this question but I thought I would point it out as a point of interest for others who might have to tread these waters at some point....

推荐答案

您可以使用自定义RMI套接字工厂执行此操作。

You can do this with a custom RMI Socket Factory.

套接字工厂为客户端和服务器端的RMI创建套接字,因此如果您自己编写,则可以完全控制所使用的端口。客户端工厂在服务器上创建,序列化,然后发送到客户端非常整洁。

The socket factories create the sockets for RMI to use at both the client and server end so if you write your own you've got full control over the ports used. The client factories are created on the server, Serialized and then sent down to the client which is pretty neat.

以下是Sun的指南,告诉您如何操作。

这篇关于如何确保RMI仅使用一组特定的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆