使用ObjectOutputStream进行socket通信的时候出现固定读到四个字节乱码的问题
问题描述:
最近在写一个通信相关的项目,服务器端和客户端通过socket进行通信。本来想利用read的阻塞特性,服务器端和客户端按照一定的流程进行文件读写。结果发现客户端或者服务器read方法一直都返回乱码。而且读到的一端可能是客户端,可能是服务器端,固定的读到前面有四个字节的乱码,后续读到的字节码都是正常的。
原因分析:
开始以为是流没有正常关闭。修改了代码确保正确关闭之后,发现即使重新启动服务器和客户端,还是会固定读到四个字节乱码。后面查资料分析才找出真正的原因:由于我实现的socket通信既有字符串通信,又有对象通信。所以我在传递字符串的时候,使用的是socket.getOutputStream得到的流。而在进行对象传输的时候,我在前面的输出流外面包裹了一层ObjectOutputStream。因为我是在一开始就对socket的输出流进行了包裹,而如果用ObjectOutputStream装饰输出流,默认的会自动在流前面带上四个字节的前缀。而因为开始我发消息只是发送字符串,所以我是直接使用socket的输出流。这就导致将前面的四个字节前缀发送出去,导致最终的乱码。具体参见下面相关代码:
/**
* Creates an ObjectOutputStream that writes to the specified OutputStream.
* This constructor writes the serialization stream header to the
* underlying stream; callers may wish to flush the stream immediately to
* ensure that constructors for receiving ObjectInputStreams will not block
* when reading the header.
*
* <p>If a security manager is installed, this constructor will check for
* the "enableSubclassImplementation" SerializablePermission when invoked
* directly or indirectly by the constructor of a subclass which overrides
* the ObjectOutputStream.putFields or ObjectOutputStream.writeUnshared
* methods.
*
* @param out output stream to write to
* @throws IOException if an I/O error occurs while writing stream header
* @throws SecurityException if untrusted subclass illegally overrides
* security-sensitive methods
* @throws NullPointerException if <code>out</code> is <code>null</code>
* @since 1.4
* @see ObjectOutputStream#ObjectOutputStream()
* @see ObjectOutputStream#putFields()
* @see ObjectInputStream#ObjectInputStream(InputStream)
*/
public ObjectOutputStream(OutputStream out) throws IOException {
verifySubclass();
bout = new BlockDataOutputStream(out);
handles = new HandleTable(10, (float) 3.00);
subs = new ReplaceTable(10, (float) 3.00);
enableOverride = false;
writeStreamHeader();
bout.setBlockDataMode(true);
if (extendedDebugInfo) {
debugInfoStack = new DebugTraceInfoStack();
} else {
debugInfoStack = null;
}
} /**
* The writeStreamHeader method is provided so subclasses can append or
* prepend their own header to the stream. It writes the magic number and
* version to the stream.
*
* @throws IOException if I/O errors occur while writing to the underlying
* stream
*/
protected void writeStreamHeader() throws IOException {
bout.writeShort(STREAM_MAGIC);
bout.writeShort(STREAM_VERSION);
}
ObjectOutputStream
解决办法:
既然直接用ObjectOutputStream将原来的socket的输出流进行包裹之后会出现固定四个字节的乱码,那么可以考虑用原来的socket输出流进行写数据的时候,接收方固定丢弃四个字节乱码。这样虽然可以实现,但是总感觉很别扭。最终我优化了相关的读写对象方法,只是用原来socket的输出流进行对象读写,具体代码实现如下:
public <T> void writeObj(T obj) throws ZSocketException {
if (obj == null) {
return;
}
try(ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut)) { // 这个只是为了计算出对象大小而用的中介输出流
objOut.writeObject(obj);
byte[] ObjByte = byteOut.toByteArray();
Header header = new Header(StringMsgType.OBJECT, ObjByte.length);
HeaderAnalyser analyser = new HeaderAnalyser(); // 先写消息头,再写消息内容
output.write(analyser.formatHeader(header), 0, Constants.HEADER_LEN);
output.write(ObjByte, 0, ObjByte.length);
output.flush(); } catch (IOException e) {
throw new ZSocketException(e);
}
} public <T> T readObj(long len, Class<T> clazz) throws ZSocketException {
if (len < 0 || clazz == null) {
throw new IllegalArgumentException("Negative read length or null object class!");
} try(ByteArrayOutputStream out = new ByteArrayOutputStream(Constants.BUFF_SIZE)) {
writeData(input, out, len);
try (ByteArrayInputStream byteIn = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream objIn = new ObjectInputStream(byteIn)) {
@SuppressWarnings("unchecked")
T result = (T) objIn.readObject();
return result;
}
} catch (Exception e) {
throw new ZSocketException(e);
} }
ObjectWriteRead
使用ObjectOutputStream进行socket通信的时候出现固定读到四个字节乱码的问题的更多相关文章
- 【转】Python学习---Socket通信原理以及三次握手和四次挥手详解
[原文]https://www.toutiao.com/i6566024355082404365/ 什么是Socket? Socket的中文翻译过来就是"套接字".套接字是什么,我 ...
- 我看不下去鸟。。。。Java和C#的socket通信真的简单吗?
这几天在博客园上看到好几个写Java和C#的socket通信的帖子.但是都为指出其中关键点. C# socket通信组件有很多,在vs 使用nuget搜索socket组件有很多类似的.本人使用的是自己 ...
- Java和C#的socket通信相关(转)
这几天在博客园上看到好几个写Java和C#的socket通信的帖子.但是都为指出其中关键点. C# socket通信组件有很多,在vs 使用nuget搜索socket组件有很多类似的.本人使用的是自己 ...
- 8.多线程和Socket通信
一.多线程 1.进程的概念: 进程就是应用程序的执行实例,有独立的内存空间和系统资源.当一个应用程序没有执行的时候,它就不是一个进程. 2.进行的特征: (1)动态性:动态产生动态消亡. (2)并 ...
- Java Socket 通信实例 - 转载
基于Tcp协议的简单Socket通信实例(JAVA) 好久没写博客了,前段时间忙于做项目,耽误了些时间,今天开始继续写起~ 今天来讲下关于Socket通信的简单应用,关于什么是Socket以及一些 ...
- 基于Tcp协议的简单Socket通信实例(JAVA)
好久没写博客了,前段时间忙于做项目,耽误了些时间,今天开始继续写起~ 今天来讲下关于Socket通信的简单应用,关于什么是Socket以及一些网络编程的基础,这里就不提了,只记录最简单易懂实用的东西. ...
- java学习小笔记(三.socket通信)【转】
三,socket通信1.http://blog.csdn.net/kongxx/article/details/7288896这个人写的关于socket通信不错,循序渐进式的讲解,用代码示例说明,运用 ...
- 坦克大战--Java类型 ---- (3)实现socket通信
一.实现思路 使用socket通信的一些方法来实现socket通信,客户端和服务端两边需要约定好通信的接口Port(尽量选高的),客户端需要服务端的IP地址,以实现数据交流. 同时,客户端和服务端需要 ...
- php简单实现socket通信
socket通信的原理在这里就不说了,它的用途还是比较广泛的,我们可以使用socket来做一个API接口出来,也可以使用socket来实现两个程序之间的通信,我们来研究一下在php里面如何实现sock ...
随机推荐
- js中的包装对象。
我们都知道在js中普通类型的变量是没有属性和方法的,然后有时却并不是这样. var str = "努力,奋斗"; console.log(str.length); var num ...
- 如何编写Angular指令
[20140917]Angular:如何编写一个指令 *:first-child { margin-top: 0 !important; } body>*:last-child { margin ...
- 剑指架构师系列-Hibernate需要掌握的Annotation
1.一对多的关系配置 @Entity @Table(name = "t_order") public class Order { @Id @GeneratedValue priva ...
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
- 每天2分钟平板支撑Plank,锻炼核心肌群,远离背疼痛
本文已转至 http://www.zhoujingen.cn/blog/2692.html 平板支撑(plank)被公认为训练核心肌群最有效的方法之一,每天坚持做可以让平坦的小腹重见天日.据说目前p ...
- [转]提高 Linux 上 socket 性能,加速网络应用程序的 4 种方法
原文链接:http://www.ibm.com/developerworks/cn/linux/l-hisock.html 使用 Sockets API,我们可以开发客户机和服务器应用程序,它们可以在 ...
- JS魔法堂:浏览器模式和文档模式怎么玩?
一.前言 从IE8开始引入了文档兼容模式的概念,作为开发人员的我们可以在开发人员工具中通过“浏览器模式”和“文档模式”(IE11开始改为“浏览器模式”改成更贴切的“用户代理字符串”)品味一番,它的出现 ...
- [ORM] Entity Framework(2) CodeFirst进阶
在上一节中,实现了CodeFirst快速入门.但是很多与数据库的细节还无法自定义.以及使用EF过程中,需要注意的事项. 在本节中,会涉及到以下 EF中的连接字符串 EF的对象状态 延迟加载,为什么需要 ...
- android 获取当前位置
1. Android开发位置感知应用程序方式:1. GPS 定位 精确度高,仅适用于户外,严重消耗电量.如果手机内置GPS接受模块,即使手机处于信号盲区,依然可以获取位置信息. 2. NETW ...
- OnMeasureItem和OnDrawItem的区别和联系
我们在做程序设计时界面与功能,那个更加吸引用户的兴趣呢?这是一个很难回答的问题.拥有美丽的外观,软件就成功了一半.界面由控件.工具栏.菜单.窗体等元素组成,对他们进行美化就能得到一个美丽的界面. 目前 ...