简体   繁体   English

Java TCP服务器-客户端设计解决方案

[英]Java TCP Server-Client Design Solution

I'm in the process of developing a highly object-oriented solution, (ie, I want as little coupling as possible, lots of reusability and modular code, good use of design patterns, clean code, etc). 我正在开发一个高度面向对象的解决方案(即,我希望尽可能少的耦合,大量的可重用性和模块化代码,良好的设计模式使用,简洁的代码等)。 I am currently implementing the client-server aspect of the application, and I am new to it. 我目前正在实现该应用程序的客户端-服务器方面,这是我的新手。 I know how to use Sockets, and how to send streams and receive them. 我知道如何使用套接字,以及如何发送和接收流。 However, I am unsure of actually how to design my solution. 但是,我不确定实际上如何设计我的解决方案。

What patterns (if any) are there for TCP Java solutions? TCP Java解决方案有哪些模式(如果有)? I will be sending lots of serialized objects over the network, how do I handle the different requests/objects? 我将通过网络发送大量序列化的对象,如何处理不同的请求/对象? In fact, how do I handle a request itself? 实际上,我如何处理请求本身? Do I wrap each object I'm sending inside another object, and then when the object arrives I parse it for a 'command/request', then handle the object contained within accordingly? 我是否将要发送的每个对象包装在另一个对象中,然后当对象到达时我将其解析为“命令/请求”,然后相应地处理其中包含的对象? It is this general design that I am struggling with. 我正在努力的就是这种总体设计。

All the tutorials online just seem to be bog-standard, echo servers, that send back the text the client sent. 在线上的所有教程似乎都是沼泽标准的回显服务器,可以将客户端发送的文本发回。 These are only useful when learning about actual sockets, but aren't useful when applying to a real situation. 这些仅在了解实际套接字时有用,而在应用于实际情况时则无用。 Lots of case statements and if statements just seems poor development. 很多案例陈述和if陈述似乎发展不佳。 Any ideas? 有任何想法吗? I'd much rather not use a framework at this stage. 在此阶段,我宁愿不使用框架。

Cheers, 干杯,

Tim. 蒂姆。

Consider using a higher level protocol then TCP/IP, don't reinvent the wheel. 考虑使用比TCP / IP更高级别的协议,不要重新发明轮子。 rmi is a good option and you should be able to find good tutorials on it. rmi是一个不错的选择,您应该可以找到关于它的不错的教程。

I suggest you either use RMI, or look at it in details so you can determine how you would do things differently. 我建议您要么使用RMI,要么对其进行详细研究,以便可以确定如何以不同的方式进行操作。 At a minimum I suggest you play with RMI to see how it works before attempting to do it yourself. 建议您至少与RMI一起玩,看看它如何工作,然后再尝试自己做。

If high performance and low latency aren't main requirements then just use existing solutions. 如果高性能和低延迟不是主要要求,则只需使用现有解决方案即可。 And if you decide to use rmi than consider using J2EE with EJB - it'll provide you a transaction management on top of rmi. 而且,如果您决定使用rmi而不是考虑将J2EE与EJB一起使用-它会为您提供基于rmi的事务管理。

Otherwise if you need extremely low latency take a look on sources of existing solutions that use custom protocols on top of tcp. 否则,如果您需要极低的延迟,请查看在tcp之上使用自定义协议的现有解决方案的来源。 For example OpenChord sends serialized Request and Response objects and Project Voldemort uses custom messages for its few operations. 例如,OpenChord发送序列化的Request和Response对象,而Voldemort项目使用自定义消息进行少量操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM