Hi,
Recently I have been trying to send and receive stuff over network. I have two computers and I was practicing with them. Sending a simple text was easy, but file was not easy.
I kept trying but failed. So, I found an example on the Internet end tried to understand it.
This is the receiver side of the code:
import java.net.*; import java.io.*; public class FileClient{ public static void main (String [] args ) throws IOException { int filesize=6022386; // filesize temporary hardcoded long start = System.currentTimeMillis(); int bytesRead; int current = 0; // localhost for testing Socket sock = new Socket("127.0.0.1",13267); System.out.println("Connecting..."); // receive file byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("source-copy.pdf"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; // thanks to A. Cádiz for the bug fix do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); long end = System.currentTimeMillis(); System.out.println(end-start); bos.close(); sock.close(); } }
They define int filesize=6022386; and use this as the array length, if I am not wrong. How do they know this?