从java中的套接字断开连接
你好 我正在构建一个小 p2p 程序,实现服务器端和客户端。 当我使用客户端程序时,首先想到它的作用是连接到其列表中的每个服务器,发送数据(关于客户端)并断开连接。下次客户端连接到这些服务器之一时,它将被识别。
我的问题 - 当我告诉客户端断开连接时,我收到此异常
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at oop.ex3.nameserver.NameServerThread.run(NameServerThread.java:24)
以断开连接我刚刚写道:
finally {
out.close();
in.close();
socket.close();
}
那么,我如何避免此异常?谢谢!
Hi
i am building a little p2p program, implementing both the server-side and the client-side.
when I lunch the client-side program, first think it does is to connect to each server in its list, send data (about the client-side) and disconnect. The next time the client-side connects to one of these servers it will be recognized.
My problem - when i tell the client-side to disconnect, i get this exception
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at oop.ex3.nameserver.NameServerThread.run(NameServerThread.java:24)
to disconnect i just wrote:
finally {
out.close();
in.close();
socket.close();
}
so, how do i avoid this exception? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Socket.close() 的 JavaDoc 明确指出:
这将引发异常,因为您已经关闭了它们!
The JavaDoc for Socket.close() states clearly:
which will throw the exception since you've already closed them!
当您关闭客户端时,您期望服务器端发生什么?
为了避免这种异常,您需要实现自己的 readUnsignedShort() 方法,例如。
When you close the client side, what would you expect to happen on the server side?
To avoid this exception, you need to implement you own readUnsignedShort() method like.
在关闭输出流之前进行刷新是否正确?:
Would it be right doing a flush before closing the output streams?: