Writing to Socket outputStream w/o closing it

eyal picture eyal · May 13, 2010 · Viewed 19.2k times · Source

I'd like to write some messages to the server. Each time, for the tramsmitting only, I'm closing the outputStream and reopen it when I have to send the next message.

os.write(msgBytes);
os.write("\r\n".getBytes());
os.flush();
os.close();

How Can I keep this Socket's OutputStream, os, open and still be able to send the message?

Thanks.

Answer

ZZ Coder picture ZZ Coder · May 13, 2010

I am missing something here. If you don't call close, it will not close. For example,

os.write(msgBytes);
os.write("\r\n".getBytes());
os.flush();
// Do something 
os.write("more message");
os.flush();
// When you are finally done
os.close();