Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 745
handicop Avatar asked Feb 28 '26 17:02

handicop


1 Answers

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.

like image 71
Bombe Avatar answered Mar 03 '26 06:03

Bombe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!