套接字关闭与输入流关闭 [英] Socket close vs Inputstream close

查看:107
本文介绍了套接字关闭与输入流关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C类扩展线程

static Queue<Socket> socketQueue
Make connection to another server or client
And then add socket to socketqueue

a类扩展线程

method a
bufferedinputstream bis = socketQueue.poll
Do work
Make bis null without closing it<br>

b类扩展线程

Method b
Bufferedinputstream bis = socketqueue.poll
Do work
Make bis null without closing it

我确实使bufferedinput流为空,因为我不想关闭连接的套接字.一些帖子告诉我,关闭输入/输出流也会关闭套接字. 每当我将输入/输出流与套接字一起使用时,通常会关闭流和套接字(如果其不为null).

I did make bufferedinput stream null since i do not want to close the connected socket. Several posts were telling me that closing input/output stream would close the socket as well. Whenever I use input/output stream with socket, I usually close stream and socket if its not null.

我要在这里做的是在不需要输入或输出流的情况下使套接字保持活动状态并重用它.

What I am trying to do here is to make the socket alive and reuse when input or output stream is needed without connecting again.

我尝试了socket.shutdowninput和output,但是,当我使用套接字创建另一个输入/输出流时,这会引发异常.

I tried socket.shutdowninput and output, however, this throws an exception when i make another input/output stream with the socket.

在这一点上我是否有误解或遗漏的东西?

Is there anything I have misunderstood or am missing at this point?

推荐答案

通过套接字进行的连接仅具有一个InputStream和一个OutputStream.一旦关闭其中任何一个(或Socket本身),连接就会自动关闭.您需要将所需的流存储在某个地方并使用它们,每次使用它们时,都无法从相同的Socket中获得它们.

A connection over a socket only ever has one InputStream and one OutputStream. As soon as you close any of those (or the Socket itself) the connection is automatically closed. You need to store the streams you need somewhere and use those, you can not get them from the same Socket each time you need them.

这篇关于套接字关闭与输入流关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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