Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Maybe your file is bigger than 1024 bytes? It's bigger than the buffer</p> <p><b>Edit: </b> The error is because of this line:</p> <pre><code>FileOutputStream fout=new FileOutputStream(path); </code></pre> <p>Comment it out because you don't really do anything with it. It's in the serverside code one of the first lines in your first try-catch </p> <p>This line opens an output-Stream to your file. Nothing happens with this stream but it's also not closed.</p> <p>This causes not only the error on the client side (because no data is transfered), but also wipes out all data you store in your file.</p> <p><b>Edit 2:</b></p> <p>I tried a picture with the bytesize of 305564</p> <p>and although the buffer on the client side was big enough i just got 65536 bytes of data transferred at the same time.</p> <p>For big files you have to use loops to get all needed data filled in to your buffer:</p> <p>I tested it out because i wanted to get it work, too.</p> <p><b>Client Side</b></p> <pre><code>try { Socket sock = new Socket("127.0.0.1", 12345); byte[] mybytearray = new byte[65536]; System.out.println("1"); InputStream is = sock.getInputStream(); System.out.println("2"); File recievedFile=new File(fileName+"_res.jpg"); System.out.println("3"); recievedFile.createNewFile(); System.out.println("4"); FileOutputStream fos = new FileOutputStream(recievedFile); System.out.println("5"); BufferedOutputStream bos = new BufferedOutputStream(fos); System.out.println("6"); int loopcount = is.read(); for(int i = 0; i &lt; loopcount; i++) { int bytesRead = is.read(mybytearray, 0, mybytearray.length); System.out.println(bytesRead); System.out.println("7"); bos.write(mybytearray, 0, bytesRead); System.out.println("8"); bos.flush(); } System.out.println("9"); bos.close(); sock.close(); } catch(Exception e) { e.printStackTrace(); } </code></pre> <p><strong>Server Side</strong></p> <pre><code> try { ServerSocket servsock=new ServerSocket(12345); Socket sock = servsock.accept(); int bufferSize = 65536; byte[] mybytearray = new byte[bufferSize]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); OutputStream os = sock.getOutputStream(); int loopcount = (int) (file.length()/bufferSize)+(file.length()%bufferSize==0?0:1); System.out.println("loopcount: "+loopcount); os.write(loopcount); os.flush(); for(int i = 0; i &lt; loopcount; i ++) { System.out.println(i); bis.read(mybytearray, 0, mybytearray.length); //System.out.println(mybytearray.length); os.write(mybytearray, 0, mybytearray.length); os.flush(); } os.close(); sock.close(); } catch(Exception e) { e.printStackTrace(); } </code></pre>
 

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