java中使用什么数据结构通过tcp/ip进行通信?
假设我想在两个使用 TCP 套接字的 Java 程序之间发送许多消息。
我认为最方便的方法是发送如下对象:
PrintStream ps = new PrintStream(s.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(ps);
some_kind_of_object_here;
oos.writeObject(some_kind_of_object_here);
ps.print(oos);
我想发送,字符串,数字,HashMap,布尔值 如何使用可以存储所有属性的 fx 1 对象来执行此操作? 我认为 ArrayList 是可序列化的,我们可以将所有内容放在那里,但这不是优雅的方式。 我想发送不同类型的数据,因为用户可以从服务器可以执行的各种选项中进行选择。 有什么建议吗?
Let's assume I want to send many messages between 2 programs made in java that use TCP sockets.
I think the most convienient way is to send objects like:
PrintStream ps = new PrintStream(s.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(ps);
some_kind_of_object_here;
oos.writeObject(some_kind_of_object_here);
ps.print(oos);
I want to send, strings, numbers, HashMaps, boolean values
How can I do this using fx 1 object that can store all that properties?
I though about ArrayList that is serializable and we can put there everything but is not elegant way.
I want to send different types of data because user can choose from a variety of options that server can do for it.
Any advices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您考虑过使用 RMI 吗?
否则,如果您需要额外的性能,请坚持使用
Serialized
甚至Externalized
。您不需要像列表这样的包装类。当然,您可以构建自己的包装对象,将所有关联数据保存在一起。此类“应用程序数据单元”将帮助您处理发送实体的正确顺序。只要所有成员都是可序列化的,一旦将包装器对象放入对象流,完整的对象图就会自动序列化。
如果您不使用包装类,只需确保您想要传输的每个对象/类型至少实现
Serialized
。然后您必须验证数据发送的顺序是否与您除了数据到达之外的顺序一致。Have you considered using RMI?
Otherwise stick with
Serializable
or evenExternalizable
, if you need extra performance.You don't need wrapper classes like Lists. Of course you can build your own wrapper object thats holds togeher all associated data. Such "application data units" will help you to take care of the right order you send the entites. As long as all member are
Serializable
, the complete object graph will be serialized automagically, once you put the wrapper object into the object stream.If you don't use wrapper classes, just be sure every object/type you want to transer implements at least
Serializable
. Then you have to verify that the order of data send corresponds with the order you except data to arrive.大师,您知道可以通过套接字将任何可序列化对象发送到另一个 JVM,是吗?
如果是这样,最简单的方法是拥有一个包含所有对象的可序列化对象,然后转发它。对象列表可能是最简单的。然后,您可以在另一端对其进行反序列化,并以您需要的任何方式处理列表中的对象。
我建议您阅读 Java 中的序列化技术,这样您就知道可以做的所有聪明的事情。
http://java.sun.com/javase/ 7/docs/technotes/guides/serialization/index.html
Master, you know you can send any serializable object across a socket to another JVM, yes?
If so, the easiest way is to have a serializable object containing all your objects and then just forward that. A list of objects is probably the most simple. You can then deserialize it on the other side, and process the objects in the list in whatever way you need.
I would suggest you read up on the serialization technology in Java, so you know all the smart things you can do.
http://java.sun.com/javase/7/docs/technotes/guides/serialization/index.html
按照您编写代码的方式,您已经可以发送任何 Java 对象。当然,这些对象包括“字符串、数字、HashMap、布尔值”。
只需写入 ByteArrayOutputStream:
The way you wrote you code, you already can send any Java object. Those objects, of course, ihclude "strings, numbers, HashMaps, boolean values".
Just write into ByteArrayOutputStream:
如果您需要保持数据包较小,您可以使用普通的
Object[]
,将您想要的任何内容放入其中并使用指定数据包类型的枚举,允许您存储多少和哪种类型的对象消息内。当然,在选择如何处理每个数据包时,您必须根据
Type t
进行切换,但由于协议通常是 FSM(这取决于您正在编码的内容),因此这会很好地工作而不会浪费太多不需要时留出空间或分配列表。If you need to keep packets small you can use a plain
Object[]
, place whatever you want inside and use an enum that specifies the packet type, allowing you to how many and which kinds of objects are store inside the message.of course you will have to switch according to
Type t
when choosing what to do with every packet, but since protocols are usually FSMs (it depends on what you are coding) this would work well without wasting too much space or allocating lists when it's not needed.