Search code examples
javasocketsnetwork-programmingou

Writing to Socket outputStream w/o closing it


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.


Solution

  • 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();