Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this will answer this question as well as <a href="https://stackoverflow.com/questions/7599742/how-do-i-recognize-eof-in-java-sockets">How do I recognize EOF in Java Sockets?</a> and <a href="https://stackoverflow.com/questions/6569995/what-can-i-send-to-an-inputstream-to-signify-eof-has-been-reached">What can I send to an InputStream to signify EOF has been reached?</a></p> <p>I had a similar problem; my dilemma was that I had a client/server request-response protocol where one of the requests included a stream sent from the client side using <code>clientProps.store()</code>. The corresponding <code>serverProps.load()</code> on the server side never returns because it needs to see the "end-of-file" - which in Java means the client has to close it's stream; resulting in the socket connection closing. The unwanted result was that, not only could I not keep the socket open for indefinite request-response exchanges, I couldn't even keep it open for the server to send its reply.</p> <p>I hated Java for making me do that, even more because the documentation for Properties.load() says:</p> <blockquote> <p>The specified stream remains open after this method returns.</p> </blockquote> <p>That could never happen if it's detecting end-of-file by seeing the stream close!! Anyway, now, I still love Java because it allowed me to use this solution (might not be useful if you have any special encoding or localization of the data you are streaming):</p> <p>I used this on the client side:</p> <pre><code> PrintWriter toServer; Properties clientProps = new Properties(); // ... code to populate the properties and to // construct toServer from the socket ... clientProps.store(toServer, null); toServer.write('\u001A'); // this is an old-school ASCII end-of-file toServer.flush(); </code></pre> <p>On the server side I extended Reader to detect the 1A and return -1 (so that the <code>serverProps.load()</code> learns about the end-of-file in the normal way (by seeing -1 returned from a call to <code>read()</code>), but <em>below</em> that, the stream and the socket stay open.</p> <pre><code> BufferedReader fromClient; Properties serverProps = new Properties(); // ... code to construct fromClient from the socket ... serverProps.load (new PropReader (fromClient)); ///// private static class PropReader extends Reader { BufferedReader src; boolean eof=false; private PropReader(BufferedReader fromClient) { super(); src=fromClient; } @Override public int read(char[] cbuf, int off, int len) throws IOException { int inCount; if (!eof) { inCount = src.read(cbuf, off, len); if (inCount &gt; 0) { // we read a buffer... look at the end for the EOF that the client used to mark the end of file if (cbuf[off+inCount-1] == '\u001A') { --inCount; // don't send eof with the data eof = true; // next time... we'll return -1 } } } else { inCount = -1; } return inCount; } @Override public void close() throws IOException { src.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload