Socket close vs Inputstream close

放肆的年华 提交于 2019-12-11 00:31:47

问题


Class c extends thread

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

Class a extends thread

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

Class b extends thread

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

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/8819711/socket-close-vs-inputstream-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!