In most protocols, the server accepts some EOF character. Send such a character instead of closing the stream.
For example, IRC servers interpret "\ r \ n" as the end of a message. This will be 2 messages on one open OutputStream:
PrintStream printStream = new PrintStream(socket.getOutputStream()); printStream.print("JOIN #channel1\r\n"); printStream.flush( ); printStream.print("JOIN #channel2\r\n"); printStream.flush( );
In addition, you must wrap your outputStream with a DataOutputStream. This shell creates more portable output. Plain OutputStream can cause problems with some primitive data types if the server and client have different computer architectures.
source share