1. objectinputstream与objectoutputstream的顺序问题
在网络通讯中,主机与客户端若使用objectinputstream与objectoutputstream建立对象通讯,必须注意声明此两个对象的顺序。
如:
主机端先建立objectinputstream后建立objectoutputstream,则对应地客户端要先建立objectoutputstream后建立objectinputstream,否则会造成两方互相等待数据而导致死锁。
原因是建立objectinputstream对象是需要先接收一定的header数据,接收到这些数据之前会处于阻塞状态。以下为java api文档的说明
creates an objectinputstream that reads from the specified inputstream.
a serialization stream header is read from the stream and verified.
this constructor will block until the corresponding objectoutputstream
has written and flushed the header.
故而为了防止这种死锁状态,通讯两方的objectinputstraem,objectoutputstream必须注意顺序对应使用。
2. objectinputstream接收到非objectoutputstream数据的问题
在使用objectinputstream与objectoutputstream对象通讯的通讯双方,假设客户端程序出现错误,发送了非objectoutputstream封装发送的数据(比如发送一个数字或字符串到主机),则主机端的objectinputstream接收到错误数据后不能自动纠正,会一直接收数据而处于阻塞状态,从而导致通讯失败。尚未找到解决方法。目前想的办法为写自己的objectstream类。
3. 解决版本问题
使用objectstream的时候会额外发送一个关于对象的序列号
static final long serialversionuid = ….
手动加入此域则可避免版本差异导致的问题。
对象序列号的计算可用sdk的serialver计算。
