Socket使用ObjectInputStream与ObjectOutputStream读写数据问题
服务端一直阻塞。
原因
ObjectInputStreamAPI: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.
意思是,创建一个ObjectInputStream,从指定流中InputStream读取数据,从流中读取序列化标头进行验证,那么这个构造函数将被阻塞,直到相应的ObjectOutputStream已经写入数据并刷新了报头。即,读取与写入是一一对应的,代码中,只需将client中的创建流的顺序更改一下即可。
教训
这个问题因为复制代码没有检查造成结果,收获,复制代码一定要认真检查,不然会付出惨痛的代价。