Java:套接字还是 RMI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2620687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Sockets or RMI?
提问by StillLearning
I need to separate our application into a light-weight gui application and a business logic application. This won't be a client/server setup as such, as the 'server' component will only have one client.
我需要将我们的应用程序分成一个轻量级的 gui 应用程序和一个业务逻辑应用程序。这不会是客户端/服务器设置,因为“服务器”组件将只有一个客户端。
The other limitation in the application is that it has only one entry/exit point. Therefore if we were to use RMI, it would only ever be on one function. All form data is already wrapped up into a string and passed through one transport area.
应用程序的另一个限制是它只有一个入口/出口点。因此,如果我们使用 RMI,它将永远只在一个函数上。所有表单数据都已经被包装成一个字符串并通过一个传输区域。
Should I just use Java Sockets to enhance this application, or go with RMI? Or some other Java technology?
我应该只使用 Java Sockets 来增强这个应用程序,还是使用 RMI?或者其他一些Java技术?
I made a previous post outlining the requirements of our application, however it went unanswered. https://stackoverflow.com/questions/2604528/terminal-panel-pc-single-server-solution-client-server-or-rdp
我之前发表了一篇文章,概述了我们申请的要求,但没有得到答复。https://stackoverflow.com/questions/2604528/terminal-panel-pc-single-server-solution-client-server-or-rdp
Cheers.
干杯。
采纳答案by chris
personally, RMI seems like a bit of overkill if you've just got one method to call, and all your data is already wrapped in a string. i imagine a simple socket server would suffice very well for your needs. however, RMI does give you a bunch of stuff for free, like multithreading, distributed garbage collection, object marshalling, etc etc. however if you only have 1 client then multithreading might not be useful and since you're doing your own object marshalling then these benefits might not gain you anything.
就个人而言,如果您只有一种方法可以调用,并且您的所有数据都已经包含在一个字符串中,那么 RMI 似乎有点矫枉过正。我想一个简单的套接字服务器就足以满足您的需求。但是,RMI 确实免费为您提供了很多东西,例如多线程、分布式垃圾收集、对象编组等。但是,如果您只有一个客户端,那么多线程可能没有用,因为您正在执行自己的对象编组这些好处可能不会给您带来任何好处。
there's a good page on rmi's capabilities here : http://java.sun.com/javase/technologies/core/basic/rmi/whitepaper/index.jsp
这里有一个关于 rmi 功能的好页面:http: //java.sun.com/javase/technologies/core/basic/rmi/whitepaper/index.jsp
回答by Omry Yadan
since your protocol is already very simple (you just pass a string) I suggest that you just go with sockets. the advantage would be that you will not be tied up to Java on both ends, for example - it will be possible to switch the UI to another language easily.
由于您的协议已经非常简单(您只需传递一个字符串),我建议您使用套接字。优点是您不会在两端都被 Java 束缚,例如 - 可以轻松地将 UI 切换到另一种语言。
回答by Donal Fellows
Having done apps that used raw sockets to communicate, that used RMI, and that used SOAP, it's easiest (by a thin hair) to use RMI but then you're strongly bound to using Java for everything. The key to why RMI is easiest is that it ensures that whole messages are sent andincludes a basic discovery framework, and yet it doesn't have the complexity of SOAP (which is a lot more complicated than everything else listed above).
完成了使用原始套接字进行通信、使用 RMI 和使用 SOAP 的应用程序后,使用 RMI 是最简单的(稍有不慎),但随后您必须对所有事情都使用 Java。RMI 最简单的关键在于它确保发送整个消息并包含一个基本的发现框架,但它没有 SOAP 的复杂性(它比上面列出的所有其他内容都复杂得多)。
回答by David Soroko
You may consider wrapping your server entry point as a servlet and doing a POST from a client.
您可以考虑将服务器入口点包装为 servlet 并从客户端执行 POST。

