gpt4 book ai didi

java - 无法创建 ObjectInputStream 对象

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:02 24 4
gpt4 key购买 nike

这是我的代码。当我运行这个程序时,我最多打印出“三个半”。 (添加打印是为了调试,因为我不知道任何其他方法。)之后,执行挂起。没有异常(exception),没有提示,什么都没有。那么我的对象创建有什么问题吗?我在网上看到的每个教程都有相同的代码,但我的不起作用。

public class Connection {
Socket socket;
ObjectInputStream iStream;
ObjectOutput outputStream;

public Connection(Socket s) {
try {
System.out.println("One");
socket = s;
System.out.println("Two");
outputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println("Three");
InputStream is = socket.getInputStream();
System.out.println("Three and a Half");
iStream = new ObjectInputStream(is);
System.out.println("Four");
} catch (IOException e) {
e.printStackTrace();
}
}
}

提前致谢。

最佳答案

它在 Javadoc 中:

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 挂起,因为它正在等待输入。您需要创建一个ObjectOutputStream并通过套接字发送数据。

关于java - 无法创建 ObjectInputStream 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23431051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com