Java ObjectInputStream 挂起
我现在感觉真的很愚蠢,伙计们……基本上我是通过本地计算机上的 TCP 连接的……当我尝试在客户端创建输入/输出流时,它不会通过创建对象输入流。什么给?这在打印 2 后停止...没有异常或任何东西...这不是我第一次使用这个类,这部分是我感到困惑的原因。
try {
System.out.println("1");
mySocket = new Socket("localhost", 11311);
System.out.println("12");
oos = new ObjectOutputStream(mySocket.getOutputStream());
System.out.println("2");
ois = new ObjectInputStream(mySocket.getInputStream());
System.out.println("13");
} catch (Exception e) {
e.printStackTrace();
}
I am feeling really stupid right now guys.... basically I am connecting over TCP on a local machine... and when I try to make the In/out streams at the client it wont get passed creating the object input stream. What gives? This stops after printing 2... no exceptions or anything... This isn't the first time I've used this class which is partialy why I am puzzled.
try {
System.out.println("1");
mySocket = new Socket("localhost", 11311);
System.out.println("12");
oos = new ObjectOutputStream(mySocket.getOutputStream());
System.out.println("2");
ois = new ObjectInputStream(mySocket.getInputStream());
System.out.println("13");
} catch (Exception e) {
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自ObjectInputStream的规范:
From the specification of ObjectInputStream:
(对于未来的读者:)我遇到了同样的问题,因为我在服务器程序中做了一个愚蠢的更改,并且很长一段时间没有测试它,然后我对为什么程序被锁定感到困惑。
ServerSocket
接受连接 (responderSocket = serverSock.accept();
),然后突然出现一个不适当的if
(我提到的愚蠢的更改!)程序跳出线程,并且因为我没有添加finally
块来关闭流和套接字,所以套接字被遗弃,无法发送或接收任何内容(甚至是流标头)。所以在客户端程序中没有流头(当我调试代码时,我看到锁定之前执行的最后一个函数是:readStreamHeader();
)所以要小心服务器端发生的事情,也许问题不在你所期望的地方!
(For future readers:) I had the same problem because i made a silly change in server program and didn't test it for a long time then i was confused about why program is locked.
ServerSocket
accepts the connection (responderSocket = serverSock.accept();
) then suddenly for a inapropriateif
(The silly change i mentioned!) program jumps out of the thread and because i didn't add afinally
block to close streams and sockets the socket was left abandoned w/o sending or recieving anything (even stream headers). So in client side program there was no stream header (When i debbugged The code i saw that the last function executed before lock was:readStreamHeader();
)So be careful about what happens in server side, maybe problem isn't where you expecting it!