通过java中的sockets发送序列化文件 [英] sending serialization file via sockets in java

查看:159
本文介绍了通过java中的sockets发送序列化文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.out.println(Java is awesome!);



我只是不能相信强大的Java是什么,它有能力不仅保存对象(和加载它们),而且与其主要目的,通过网络发送它们。这正是我必须做的,因为我正在进行beta测试。在这个beta测试中,我给测试人员一个版本的游戏,将数据保存为对象在大多数人不知道的位置(我们是开明的人哈哈哈)。这将工作精细和dandy,除了它不是为了长期持久。但是,我可以收集他们的 record.ser counter.bin 文件(后者告诉我有多少个对象 record.ser )通过一些客户端/服务器与套接字的交互(我不知道,直到我开始阅读它,但我仍然觉得无知)。我在网上看到的大多数例子(这一个例子: http://uisurumadushanka89.blogspot.com/2010/08/send-file-via-sockets-in-java.html )将文件作为字节流发送,即一些ObjectOutputStream和ObjectInputStream 。这正是我目前的游戏版本用来保存/加载GameData。



对不起,这个冗长的介绍,但你知道我会做什么(逐步,所以我可以UNDERSTAND)实际发送整个文件。

解决方案

它很简单,其实。只需让你的对象可序列化,并创建一个ObjectOutputStream和ObjectInputStream连接到你有任何基础流,比如FileInputStream等。然后只需write()任何对象你想要的流,并阅读它在另一边。 >

使用示例



对于套接字,它将像

  ObjectOutputStream objectOut = new ObjectOutputStream(serverSocket.getOutputStream()); 
ObjectInputStream objectIn = new ObjectInputStream(clientSocket.getInputStream());


System.out.println("Java is awesome!");

Pardon my enthusiasm; I just can't believe how powerful Java is, what with its ability to not only save objects (and load them), but also with its main purpose, to send them over a network. This is exactly what I must do, for I am conducting a beta-test. In this beta-test, I have given the testers a version of the game that saves the data as Objects in a location most people don't know about (we are the enlightened ones hahaha). This would work fine and dandy, except that it isn't meant for long-term persistence. But, I could collect their record.ser and counter.bin files (the latter tells me how many Objects are in record.ser) via some client/server interaction with sockets (which I know nothing about, until I started reading about it, but I still feel clueless). Most of the examples I have seen online (this one for example: http://uisurumadushanka89.blogspot.com/2010/08/send-file-via-sockets-in-java.html ) were sending the File as a stream of bytes, namely some ObjectOutputStream and ObjectInputStream. This is exactly what my current version of the game is using to save/load GameData.

Sorry for this long-winded intro, but do you know what I would have to do (steps-wise, so I can UNDERSTAND) to actually send the whole file. Would I have to reconstruct the file byte-by-byte (or Object-by-Object)?

解决方案

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.

Heres an example for you.

For sockets it will be something like

ObjectOutputStream objectOut = new ObjectOutputStream(serverSocket.getOutputStream());
ObjectInputStream objectIn = new ObjectInputStream(clientSocket.getInputStream());

这篇关于通过java中的sockets发送序列化文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆