如何创建一个一次只接受一个连接的 rmi 服务器?
我正在使用 rmi java 编写一个客户端-服务器对。我希望服务器侦听连接,当一个客户端连接时,服务器应拒绝任何其他尝试连接的客户端。
I'm writing a client-server pair in java using rmi java. I want the server to listen for a connection, and while one client is connected the server should reject any other clients that try to connect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 http://download.oracle.com/javase/6/docs/api/java/rmi/registry/LocateRegistry.html#createRegistry%28int,%20java.rmi.server.RMIClientSocketFactory,%20java.rmi.server .RMIServerSocketFactory%29,并编写自定义的RMIServerSocketFactory 返回仅接受的 ServerSocket单个连接。
编辑:混搭 LocateRegistry.createRegistry 和 http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/Server.java 中添加了一些额外的代码(请注意,我没有编译此代码) ,因此您需要自己解决所有编译错误;它旨在向您展示一般用法):
编辑2:修复它以纳入@EJP的建议(有关更多详细信息,请参阅这个)。
You would need to start the RMI registry in code using http://download.oracle.com/javase/6/docs/api/java/rmi/registry/LocateRegistry.html#createRegistry%28int,%20java.rmi.server.RMIClientSocketFactory,%20java.rmi.server.RMIServerSocketFactory%29, and write a custom RMIServerSocketFactory that returns a ServerSocket that only accepts a single connection.
EDIT: with a mashup of LocateRegistry.createRegistry and http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/Server.java with a little extra code thrown in (note that I didn't compile this, so you will need to sort out any compile errors yourself; it is intended to show you the general usage):
EDIT 2: fixed it to incorporate @EJP's suggestion (for more detail see this).
一种简单的本土方法是通过单个方法传递所有内容,该方法跟踪请求线程的数量。如果已经有一个请求线程正在进行中,则可能会引发异常。否则,请求线程被允许继续。
一些代码来说明这个概念,但不一定是实现......
}
One simple home-grown approach would be to pass everything through a single method which keeps track of the number of requesting threads. If there is already a requesting thread in progress, an exception can be thrown. Otherwise, the requesting thread is allowed to proceed.
Some code to illustrate the concept but not necessarily an implementation ...
}
您在这里所要做的就是将您的远程方法声明为同步的。那么一次只有一个客户可以输入它/他们。
All you have to do here is declare your remote method(s) as synchronized. Then only one client can enter it/them at a time.