50971

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?

Answer1:

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.

Recommend