Note that there are some explanatory texts on larger screens.

plurals
  1. POJava NIO, reads not completing
    primarykey
    data
    text
    <p>I am attempting to write a simple HTTP async server and a test client. My test client makes a bunch of requests to the server, but doesn't do anything with the response except read it. The problem I'm running into is after a bunch of successful responses, my client fails to read the full response of a particular request. I have no idea why this is happening.</p> <p>Here is the read/write handler for my server: <a href="http://pastebin.com/RY7JS6kk" rel="nofollow">read/write handler</a> </p> <p>The important code is the function that sends the file:</p> <pre><code> public void handleWrite(SelectionKey key) throws IOException { client.write(out); // Write the headers to the stream if (state == State.SENDING_RESPONSE &amp;&amp; out.remaining() == 0) { // We are done writing the headers if (sendFile &amp;&amp; (mapped == null)) { // Send the file via direct transfer System.err.println("DT: " + fPath + " " + pos + "/" + fileSize); long transferred = f.transferTo(pos, fileSize, client); pos += transferred; System.err.println("DT: " + fPath + " " + pos + "/" + fileSize); } else if (mapped != null){ // Send the file (in mapped from either filesystem or from // cache) System.err.println("MAPPED: " + fPath + " " + mapped.position() + "/" + mapped.limit()); written += client.write(mapped); System.err.println("MAPPED: " + fPath + " " + written + "/" + mapped.limit()); } } if (out.remaining() == 0 &amp;&amp; ((mapped == null &amp;&amp; pos == fileSize) || (mapped != null &amp;&amp; mapped .remaining() == 0))) { // We are done transferring the file! System.err.println(pos + " " + fileSize); assert (pos == fileSize) || (mapped.position() == mapped.limit()) : "File not sent."; // Must reset position if from cache if (inCache) { mapped.position(0); } if (sendFile) { cache(); f.close(); inputStream.close(); } state = State.SOCKET_CLOSED; d.getKey(client).cancel(); client.close(); } } </code></pre> <p>and here is the response handler in the client: <a href="http://pastebin.com/ZCQx0aPw" rel="nofollow">response handler</a></p> <p>Important code is the read part:</p> <pre><code> if((sz = header.get("content-length")) != null) { bodySize = Integer.parseInt(sz); byte[] buff = new byte[bodySize]; int k = 0, read = 0; while((k = connection.getInputStream().read(buff, read, bodySize - read)) != -1 &amp;&amp; read &lt; bodySize) { read += k; } assert read == bodySize : "Not all of file read. " + read + " " + bodySize; } </code></pre> <p>The assertion at the bottom of the response handler is the one that's failing. I've also included the server and dispatcher themselves, as well as the client if thats helpful. <a href="http://pastebin.com/cp53Tigk" rel="nofollow">server/dispatcher</a> <a href="http://pastebin.com/7t6WX6LG" rel="nofollow">client</a></p>
    singulars
    1. This table or related slice is empty.
    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. 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