简体   繁体   English

Java 服务器/客户端中的编组和解组

[英]Marshalling and Unmarshalling in Java Server/Client

I have some questions on how to set up a server with marshalling and unmarshalling capabilities.我有一些关于如何设置具有编组和解组功能的服务器的问题。 I have been given code by my instructor to implement the marshalling and unmarshalling, as well as sending and receiving the data through TCP.我的导师给了我代码来实现编组和解组,以及通过 TCP 发送和接收数据。 Here is my code.这是我的代码。

First , since I need to send/receive for both a server and client I assume I need to have the files (TCPSender.java, TCPReceiver.java, and WireFormatWidget.java - the marshalling/unmarshalling class) in the same source path as both the client and the server.首先,因为我需要为服务器和客户端发送/接收我假设我需要在与两者相同的源路径中拥有这些文件(TCPSender.java、TCPReceiver.java 和 WireFormatWidget.java - 编组/解组类)客户端和服务器。 Based upon the code, that seems as if that's all I need to do.根据代码,这似乎就是我需要做的全部。 I don't seem to have to create any packages.我似乎不必创建任何包。 Does that look right?看起来对吗?

Second , since my server/client(s) will be running a marshalling/unmarshalling class, I can output data via string, and then the WireformatWidget will translate it in between the console print-outs.其次,由于我的服务器/客户端将运行编组/解组类,我可以通过字符串输出数据,然后 WireformatWidget 将在控制台打印输出之间转换它。

Last I'm not 100 percent sure on the value of sending via byte arrays, could someone give me a run-down of why this is useful?最后,我对通过字节数组发送的价值不是 100% 确定,有人可以给我一个为什么这很有用的简要说明吗? Also, perhaps send me to a their trusted source on learning about them?另外,也许可以将我发送到他们可信赖的来源以了解他们? I am consulting Java docs at the moment, and it is helpful, but I would like something more specific to my application.我目前正在咨询 Java 文档,这很有帮助,但我想要一些更适合我的应用程序的内容。 Thank you!谢谢!

I think you'll need a Widget to read/write between your client(s) and server.我认为您需要一个Widget来在您的客户端和服务器之间进行读/写。

I'd suggest using Externalizable if you want to control the marshalling / unmarshalling.如果您想控制编组/解组,我建议使用Externalizable There's also the marker interface Serializable if you want to let Java automatically control the marshalling / unmarshalling.如果您想让 Java 自动控制编组/解组,还有标记接口Serializable

As for why you might need to convert the state of a Widget to a byte[] ;至于为什么您可能需要将Widget的状态转换为byte[] it's generally so you can write them to and/or read them from somewhere (like a disk, or a remote client).通常,您可以将它们写入和/或从某个地方(如磁盘或远程客户端)读取它们。 Basically, it is one mechanism to restore the state of an instance programatically.基本上,它是一种以编程方式恢复实例状态的机制。 Other such mechanism involves generating human readable representation (eg XML and JSON ).其他此类机制涉及生成人类可读的表示(例如XMLJSON )。

Finally, for better help faster, please include your code in your question.最后,为了更快地获得更好的帮助,请将您的代码包含在您的问题中。 Making it a link to an off-site image makes it unlikely to be read.将其作为指向场外图像的链接使其不太可能被阅读。

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

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