sockets - Why code shows "Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connection." -
i building http web server in java.
if client request file , file on place in server, server gives file client. made code, , works fine.
the part of code, shows above functionality,
file targ = [contains 1 file] printstream ps; inputstream = new fileinputstream(targ.getabsolutepath()); while ((n = is.read(buf)) > 0) { system.out.println(n); ps.write(buf, 0, n); } but make code optimized, replace code below code,
inputstream = null; bufferedreader reader = null; string output = null; = new fileinputstream(targ.getabsolutepath()); reader = new bufferedreader(new inputstreamreader(is)); while( (output = reader.readline()) != null) { system.out.println("new line"); //system.out.println(output); ps.print(output); } but shows 1 error why code shows "error 354 (net::err_content_length_mismatch): server unexpectedly closed connection.". didn't understand, why shows error. error weird, because server shows 200 code, means, file there.
help me please.
edit no. 1
char[] buffer = new char[1024*16]; int k = reader.read(buffer); system.out.println("size : " + k); { system.out.println("\tsize : " + k); //system.out.println(output); ps.println(buffer); }while( (k = reader.read(buffer)) != -1 ); this prints file, bigger files, shows unreadable characters.
it shows below output (snapshot of client browser)
you output = reader.readline() data, omits newline characters. ps.print(output), newline characters not sent client.
say read this
hello\r\n world\r\n then send this:
content-length: 14 helloworld and close connection, confusing browser still waiting other 4 bytes.
i guess you'll have use ps.println(output).
you have seen if monitoring network traffic, can prove quite useful when writing or debugging server supposed communicate using network.
anyway cause trouble if newlines of file , system have mismatch (\n vs \r\n). have file:
hello\r\n world\r\n its length 14 bytes. when system treats newline when printing \n, code println() print this:
hello\n world\n which 12 bytes, not 14. better print read.
Comments
Post a Comment