简体   繁体   English

如何在RMI中的服务器上正确注册客户端?

[英]How do I properly register clients on a server in RMI?

I'm currently working on a small project that manages attendance for a summer program that I'm helping run. 我目前正在做一个小项目,该项目负责管理一个我正在帮助运行的暑期课程的出勤情况。 The RMI server is run from the office and has a specific port set for it by using the following code: RMI服务器从办公室运行,并使用以下代码为其设置了特定的端口:

Registry registry = LocateRegistry.createRegistry(4051);
RemoteManager stub = (RemoteManager) UnicastRemoteObject.exportObject(theServer, 4051);
registry.rebind(SERVER_NAME, stub);

where theServer is a reference to the object that will serve as the system's server and SERVER_NAME is simply a static String used to represent the server. 其中theServer是对将用作系统服务器的对象的引用,而SERVER_NAME只是用于表示服务器的静态String As is shown the registry binds the server to port 4051 so that clients can query for it. 如图所示,注册表将服务器绑定到端口4051以便客户端可以查询它。 Now I'm working on cross-communication between the server and multiple client instances (12 teachers running the same client program) and currently it is set such that the clients send stubs of themselves to the server for client registration. 现在,我正在服务器和多个客户端实例(运行相同客户端程序的12位教师)之间进行交叉通信,并且当前已设置为客户端将自己的stubs发送到服务器以进行客户端注册。

public void registerClient(INTERFACE_Client teach) throws RemoteException {
.
.
.
}

where INTERFACE_Client extends the Remote interface and the client is sent down by calling: 其中INTERFACE_Client扩展了Remote接口,并且通过调用以下命令来发送客户端:

server.registerClient((INTERFACE_Client) UnicastRemoteObject.exportObject(this, 4052));

where I just chose port 4052 because I didn't know what I was doing. 我之所以选择了4052端口,是因为我不知道自己在做什么。 I also didn't bind the stub to a registry. 我也没有将存根绑定到注册表。 Is a good way to do it? 这是个好方法吗? Because I've hardcoded the client port, all 12 teachers will be exporting the object using port 4052 (at least that's how I see it) from their respective client instances. 因为我已经对客户端端口进行了硬编码,所以所有12位教师都将使用端口4052(至少是我所看到的)从各自的客户端实例中导出对象。 Should the ports be randomized? 是否应将端口随机化? Should I not include a port (there's a method that doesn't require that as well)? 我是否应该不包括端口(有一种方法也不需要)? Any helpful hints would be greatly appreciated. 任何有用的提示将不胜感激。

As a disclaimer, I do need a reference to the client in the server so that people in the office can send messages to the teachers. 作为免责声明,我确实需要引用服务器中的客户端,以便办公室中的人员可以向教师发送消息。 (Server -> Client communication). (服务器->客户端通信)。 The client already has the reference to the server because it can look it up on the registry. 客户端已经具有对服务器的引用,因为它可以在注册表中查找它。
Any suggestion? 有什么建议吗?

I just chose port 4052 because I didn't know what I was doing. 我只选择了4052端口,因为我不知道自己在做什么。

That's OK, as long as port 4052 is open in both directions in both intermediate firewalls, if there are any firewalls. 没关系,只要在两个中间防火墙中双向打开端口4052(如果有的话)。 There's no real reason why you can't use the RMI Registry port 1099 for this, at both ends: it's already reserved by IANA, unless the hosts already contain some other RMI application. 两端都没有理由不能使用RMI注册表端口1099:IANA已经保留了该端口,除非主机已经包含一些其他RMI应用程序。

I also didn't bind the stub to a registry. 我也没有将存根绑定到注册表。 Is a good way to do it? 这是个好方法吗?

That's OK. 没关系。 You don't need a Registry at the client. 您不需要客户端上的注册表。 All you need is your existing registration API. 您只需要现有的注册API。

Because I've hardcoded the client port, all 12 teachers will be exporting the object using port 4052 (at least that's how I see it) from their respective client instances. 因为我已经对客户端端口进行了硬编码,所以所有12位教师都将使用端口4052(至少是我所看到的)从各自的客户端实例中导出对象。

That's OK as long as the 'client instances' are all separate TCP hosts. 只要“客户端实例”都是单独的TCP主机,就可以。

Should the ports be randomized? 是否应将端口随机化?

Not unless you have > 1 client per TCP host. 除非每个TCP主机有1个以上的客户端,否则不会这样。

Should I not include a port (there's a method that doesn't require that as well)? 我是否应该不包括端口(有一种方法也不需要)?

No. 没有。

Any helpful hints would be greatly appreciated. 任何有用的提示将不胜感激。

Your clients must implement a remote interface and be exported and then registered with the server via your existing API. 您的客户端必须实现一个远程接口并被导出,然后通过现有API在服务器中注册。 You're doing well. 你过得很好 As long as there are no firewalls, or they are all under your control, you will be OK. 只要没有防火墙,或者它们都在您的控制之下,您就可以了。

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

相关问题 如何在JBoss的默认RMI注册表中注册服务器并从另一个JVM中运行的客户端访问它? - How do I register a server in the default RMI registry for JBoss and access it from a client running in another JVM? 客户端未向Eureka服务器注册 - Clients do not register with Eureka server 具有许多客户端的RMI服务器群集 - RMI Server Clustering with many clients 如何以编程方式停止 RMI 服务器并通知所有客户端 - How to stop RMI server programmatically and notify all clients 如何使RMI服务器对本地客户端(在同一网络内)和外部客户端都起作用 - How to make a RMI server work for both local clients (within the same network) and external clients 如何在Java RMI中使用可序列化对象和回调方法正确模仿传递引用? - How do I properly mimic pass-by-reference in Java RMI with serializable objects and a callback method? 如何用一台服务器做多个客户端? - How to do multiple clients with 1 server? 如何向多线程服务器上的所有客户端发送消息? - How do I send a Message to all Clients on a multithreaded Server? 如何在Java中设置RMI? - How do I set up RMI in java? 如何确保RMI操作的原子性? - How do I ensure Atomicity of RMI Operations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM