Java Socket Programming Buffer for Large File -


I am trying to transfer a large file to the client from a server. My code still works, but if I set the buffer size in the client code to the exact size of the file, I will not always know what the file size will be. I find examples that claim that someone No matter what the size of file or buffer is because it will continue to read from the input stream ...? However, when I apply the code, which is assumed that it transfers 0 bytes.

Customers:

  Public static zero main (string [] args) IOException throws {int bytesRead; Int current = 0; FileOutputStream fos = Faucet; BufferOperputStream Boss = Faucet; Socket sock = null; Try {sock = new socket (hostname, 20000); Println ("Connect ..."); // Get the file InputStream = sock.getInputStream (); Fos = new FileOutputStream (FILE_TO_RECEIVE); Bose = new BufferedOutputStream (fos); //////////// In this place //////////////////////////// byte [] buffer = new byte [ buffer size]; Bitesrad = is Reed (buffer, 0, buffer lamps); Current = bytes read; Do {BitsRed = is Read (buffer, running, (buffer, long-current)); If (bytes read> = 0) {current + = bytesRead;}} while (bytes read> 0); boss. Write (buffer, 0, current); /////////////////////////////////// // With this: // int count; // byte [] buffer = new byte [8192]; // while ((count = is.read (buffer)) gt; 0) // {// bos.write (buffer, 0, count); //} //////////// TRANSFER 0 bytes /////////// bos.flush (); System.out.println ("File" + FILE_TO_RECEIVE + "downloaded (read" + current + "bytes)"); } Finally {if (fos! = Null} {fos.close ();} if (bos! = Null) {bos.close (;;}} if (silence! = Null) {sock.close ();}} }   

Server:

  Public static zero main (string [] args throws IOException {fileInputStream fis = null; Buffardinputstream bis = null; OutputStream OS = Faucet; Server socket srsock = null; Socket sock = null; Try {servsock = New ServerSet (20000); While {true} {System.out.println ("Waiting ..."); Try {sock = servsock.accept (); System.out.println ("Accepted Connection:" + Sock); // file file myFile = new file (FILE_TO_SEND); Byte [mybytearray = new byte [(int) myFile.length ()]; Fis = New FileInputStream (myFile); BIS = new buffer inputstream (FIS); Bis.read (mybytearray, 0, mybytearray.length); OS = sock.getOutputStream (); System.out.println ("Sending" + FILE_TO_SEND + "(" + mybytearray.length + "bytes)"); Os.write (mybytearray, 0, mybytearray.length); Os.flush (); Println ("complete."); } Finally {if (bis! = Null) {bis.cool};} if (os! = Null) {osclos ();} if (present! = Null) {sock.close ();}}}} Finally {if (servsock! = Null) {servsock.close ();   

Both ends are different, and both are nonsense. None of these are also a loop. Try it:
  while ((count = in.read (buffer)) gt; 0] {out.write (Buffer, 0, count); }   

This is the size of any buffer and gt; = Works for a byte.

Use it on both ends.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -