网络上的Java序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/707987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:35:11  来源:igfitidea点击:

Java serialization over network

javaserializationnetwork-programming

提问by elbaid

Just want to know if there's a tutorial or a how-to for serializing objects, putting them into a stream over network, and deserialize it on the other side. I understand the principles of serialization, I/O, streams, sockets, and so on, I just would like an example of a client sending an object to a server to start with.

只是想知道是否有用于序列化对象、通过网络将它们放入流中并在另一端反序列化的教程或操作方法。我了解序列化、I/O、流、套接字等的原理,我只想从一个客户端向服务器发送对象的示例开始。

采纳答案by Brian Agnew

This(pdf) is a useful tutorial which walks you through the basics of serialisation, and sockets, then ties the two concepts together (about halfway through the slides) to show how to serialise an object and send it from client to server (no RMI). I think that's precisely what you want.

这个(pdf) 是一个有用的教程,它引导您了解序列化和套接字的基础知识,然后将这两个概念联系在一起(大约在幻灯片的一半)以展示如何序列化对象并将其从客户端发送到服务器(没有 RMI )。我认为这正是你想要的。

回答by John Ellinwood

Its pretty simple, actually. Just make your objects serializable, and create an ObjectOutputStream and ObjectInputStream that are connected to whatever underlying stream you have, say FileInputStream, etc. Then just write() whatever object you want to the stream and read it on the other side.

其实很简单。只需让您的对象可序列化,并创建一个 ObjectOutputStream 和 ObjectInputStream,它们连接到您拥有的任何底层流,例如 FileInputStream 等。然后只需 write() 任何您想要的对象到流并在另一端读取它。

Heres an examplefor you.

这里有一个例子给你。

回答by Thilo

Java provides (binary) object serialization using the ObjectOutputStream(and ObjectInputStream). You can just writeObject() into the stream and readObject() on the other end. All you need to do for this to work is implement the Serializable interface.

Java 使用ObjectOutputStream(和ObjectInputStream)提供(二进制)对象序列化。您可以将 writeObject() 写入流并在另一端 readObject() 。您需要做的就是实现 Serializable 接口。

But rather than doing that manually, you may be interested in taken it one level up and using Remote Method Invocation. With RMI you can call methods on objects that live in another JVM, and all the serialization and networking happens under the hood.

但不是手动执行此操作,您可能有兴趣将其提升一级并使用Remote Method Invocation。使用 RMI,您可以调用位于另一个 JVM 中的对象上的方法,并且所有序列化和网络都发生在幕后。

And for the sake of completeness, there is also XML bean serialization, if you cannot use the binary format. That XML format is very generic (read: verbose and ugly), but there are some popular libraries (like XStream) that create alternative XML serializations.

为了完整起见,如果您不能使用二进制格式,还有XML bean 序列化。这种 XML 格式非常通用(阅读:冗长和丑陋),但有一些流行的库(如XStream)可以创建替代的 XML 序列化。

回答by Prashant Gautam

you can create object streams using the java api and send any serializable object. but youll have to mind that these go unencrypted through the network:

您可以使用 java api 创建对象流并发送任何可序列化的对象。但你必须记住,这些通过网络未加密:

on the sender's side:

在发送方:

CustomObject objectToSend=new CustomObject();
Socket s = new Socket("yourhostname", 1234);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(objectToSend);
out.flush();

and on the receiving end:

在接收端:

ServerSocket server = new ServerSocket(1234);
Socket s = server.accept();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
CustomObject objectReceived = (CustomObject) in.readObject();