新的 ObjectInputStream 块尽管刷新了 OutputStream

new ObjectInputStream blocks despite flushing OutputStream

目前我正在尝试获得一些网络体验,但在使用 ObjectIn/OutputStreams 时我无法通过连接建立过程中的某个点。

我在几个线程(比如这个:Java Creating a new ObjectInputStream Blocks)中阅读了有关 ObjectInputStream 阻塞的构造函数的问题,许多人建议先刷新输出,所以我想到了这个:

out = new ObjectOutputStream(socket.getOutputStream());
String test = "test"; 
out.writeObject(test);
out.flush();
InputStream is = socket.getInputStream();
in = new ObjectInputStream(is);

阻塞线是最后一条。 有什么建议为什么它仍然无法完成吗?感谢任何帮助。

您指定的代码行将阻塞,直到对等方在其套接字上创建 ObjectOutputStream,或向套接字写入其他内容(这将导致 StreamCorruptedException)。