简体   繁体   English

Java 套接字编程:处理多种类型的消息

[英]Java Socket Programming: Dealing with multiple types of messages

I'm developing a simple Multicast networked program, and was just curious on the best class structure and OOD patterns that best suit a client/server or client/client network.我正在开发一个简单的多播网络程序,只是对最适合客户端/服务器或客户端/客户端网络的最佳 class 结构和 OOD 模式感到好奇。 My dilemma is that I will be sending different kind of messages via datagram, and the receiver just sees a bunch of bytes coming at them.我的困境是我将通过数据报发送不同类型的消息,而接收者只会看到一堆字节。 Now, I already implemented a sort of "ID" placeholder as the first byte of all of my byte arrays to distinguish between a message containing "hello world" and one containing the coordinates of a user for instance.现在,我已经实现了一种“ID”占位符作为我所有字节 arrays 的第一个字节,以区分包含“hello world”的消息和包含用户坐标的消息。 The only option seems to be to just have a huge set of case statements in my "receive" method based on what the "ID" is but this seems like bad practice.唯一的选择似乎是根据“ID”是什么,在我的“接收”方法中只包含大量案例陈述,但这似乎是不好的做法。 Just looking for ideas to take advantage of Java's OOD patterns and all-around good coding practice.只是寻找利用 Java 的 OOD 模式和全方位良好编码实践的想法。

On a side note (I think this is somewhat related...) would it be advantageous for me to ues object streams instead?在旁注(我认为这有点相关......)对我来说使用 object 流是否有利? It seemed to me that I would still be checking instancof on everythign coming in. Thanks!在我看来,我仍然会检查所有进入的实例。谢谢!

There are several possibilities, but the Strategy Pattern seems to fit the bill best.有几种可能性,但战略模式似乎最适合。

In your situation, I would create an interface with at least two methods one to determine if the class can handle the message (which will check your ID bits) and another to actually process the message.在您的情况下,我将使用至少两种方法创建一个接口,一种是确定 class 是否可以处理消息(这将检查您的 ID 位),另一种是实际处理消息。 Then you create a separate message processing class for each type of message.然后为每种类型的消息创建一个单独的消息处理 class。

Your incoming message handler would then have a set or list of these message processing objects (typically each of a different class, but all of whom implement the interface).然后,您的传入消息处理程序将具有这些消息处理对象的集合或列表(通常每个不同的 class,但它们都实现了接口)。 When a message is received, the message handler would iterate through the message processing objects until one can handle the message is found, giving an error if no message processor will handle the message.当接收到消息时,消息处理程序将遍历消息处理对象,直到找到可以处理该消息的对象,如果没有消息处理器将处理该消息,则给出错误。

There are a few options here, depending on circumstances.这里有几个选项,视情况而定。

  1. You can certainly send serialized objects, one for each message type.您当然可以发送序列化对象,每种消息类型一个。 Serialized Java objects typically only work between Java apps, but you can aslo try protocol buffers for interoperable Objects (http://code.google.com/p/protobuf/)序列化的 Java 对象通常仅在 Java 应用程序之间工作,但您也可以尝试用于可互操作对象的协议缓冲区 (http://code.google.com/p/protobuf/)

  2. You can send documents in an interoperable format like JSON or XML您可以以可互操作的格式发送文档,例如 JSON 或 XML

  3. You can make up your own format, and parse the text as you like可以自己编格式,随意解析文本

me, i would probably go with Protocol Buffers我,我可能会使用协议缓冲区 go

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

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