tags:

views:

33

answers:

2

Hi,

I'm trying to download a file using socket and server in java.

myClient = new Socket(address,port);
   myClient.setSoTimeout(MyFileManager.TIME_OUT);
   in = new DataInputStream(myClient.getInputStream());
   out = new DataOutputStream(myClient.getOutputStream());
File requestedFile = new File(_fileManager.getDir()+fileName); //creating the new file
 //         requestedFile.createNewFile(); //now it does
          fos = new FileOutputStream(requestedFile);
          long size = in.readLong(); //get the size
          for (int i=1; i<=size; i++) {
           try {
            fos.write(in.read());
           }
           catch (IOException e) {
            e.printStackTrace();
           }
          }

I'm sending the other side the file size and then sending each byte, right before the bytes end, it throws the above exception, saying conncetion reset.

what could be the problem?

Thank you!

A: 

I think that the loop should be something like:

 for (int i=0; i<size; i++)...

Since streams are 0 based.

Also, you might want to keep reading until you hit the EOF rather than reading a specific number of bytes. See this tutorial to learn how :)

npinti