关闭 Input/OutputStream 和直接关闭 Socket 有什么区别? [英] What is the difference between closing Input/OutputStream and closing Socket directly?

查看:20
本文介绍了关闭 Input/OutputStream 和直接关闭 Socket 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道当我们对与套接字关联的 inputStream 和 outStream 调用 close 时,java 做了什么.与socket上的close调用有什么区别,即Socket.close().

I just wondering what java does when we call close on the inputStream and outStream associated with a socket. What is the difference from the close call on the socket, i.e Socket.close().

如果我们只是关闭了socket上的io流,没有关闭socket,那我们可以重新打开socket上的io流吗?

if we just close the io stream on the socket, but not close the socket, can we reopen the io stream on the socket again?

提前致谢!

推荐答案

您应该关闭从套接字创建的最外层输出流.那会冲洗它.关闭套接字或输入流都没有这样做,所以它是不够的.关闭该输出流后,您无需执行任何其他操作.

You should close the outermost output stream you have created from the socket. That will flush it. Closing either the socket or the input stream doesn't do that so it isn't adequate. Having closed that output stream you don't need to do anything else.

这篇关于关闭 Input/OutputStream 和直接关闭 Socket 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆