简体   繁体   中英

Socket close vs Inputstream close

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM