I want to download all files from PC's folder from my android for this m doing java sockets but achieved 1st file correctly others are garbage -


*i have have folder in pc in c:/ name share , in have 4 pictures after running client , server code got 4 picture downloaded in android emulator first image correctly download other 3 garbage here code server side

public class multiplefilessending  {       public static void main(string[] args) throws ioexception,eofexception     {           fileoutputstream fos;           bufferedoutputstream bos;           outputstream output;             int len;           int smblen;            inputstream in;           boolean flag=true;           datainputstream clientdata;           bufferedinputstream clientbuff;           system.out.println("waiting connection");         serversocket serversocket = new serversocket(5991);           socket clientsocket = null;           clientsocket = serversocket.accept();          ////////////////////////         file myfile = new file("c:/share");           file[] files = myfile.listfiles();                        outputstream os = clientsocket.getoutputstream();             dataoutputstream dos = new dataoutputstream(os);             dos.writeint(files.length);            (int count=0;count<files.length;count ++)         {                 dos.writeutf(files[count].getname());            }            (int count=0;count<files.length;count ++)         {                  int filesize = (int) files[count].length();                 dos.writeint(filesize);           }            (int count=0;count<files.length;count ++)         {             int filesize = (int) files[count].length();           byte [] buffer = new byte [filesize];            //fileinputstream fis = new fileinputstream(myfile);             fileinputstream fis = new fileinputstream(files[count].tostring());             bufferedinputstream bis = new bufferedinputstream(fis);              //sending file name , file size server             bis.read(buffer, 0, buffer.length); //this line important            dos.write(buffer, 0, buffer.length);              dos.flush();            //dos.close();           }              if (flag==false){               clientsocket = serversocket.accept();               flag = true;            }         //closing socket             //dos.close();           clientsocket.close();      }    } 

and client side

socket sock = new socket("10.0.2.2", 5991);
system.out.println("connecting.........");

        fileoutputstream fos;           bufferedoutputstream bos;           outputstream output;         dataoutputstream dos;         int len;           int smblen;            inputstream in;           boolean flag=true;           datainputstream clientdata;           bufferedinputstream clientbuff;          while (true)         {               //while(true && flag==true){               while(flag==true)             {                     in = sock.getinputstream(); //used                     clientdata = new datainputstream(in); //use                    clientbuff = new bufferedinputstream(in); //use                     int filesize = clientdata.read();                   arraylist<file>files=new arraylist<file>(filesize);                         arraylist<integer>sizes = new arraylist<integer>(filesize); //store file size client                   //start accept filename server                   (int count=0;count < filesize;count ++){                       file ff=new file(clientdata.readutf());                       files.add(ff);                   }                   (int count=0;count < filesize;count ++){                       sizes.add(clientdata.readint());                   }                   (int count =0;count < filesize ;count ++)                 {                         if (filesize - count == 1)                     {                           flag =false;                       }                       len=sizes.get(count);                       //system.out.println("file size ="+len);                       output = new fileoutputstream("/mnt/sdcard/" + files.get(count));                       dos=new dataoutputstream(output);                       bos=new bufferedoutputstream(output);                       byte[] buffer = new byte[1024];                         bos.write(buffer, 0, buffer.length); //this line important                       while (len > 0 && (smblen = clientdata.read(buffer)) > 0)                     {                            dos.write(buffer, 0, smblen);                            len = len - smblen;                           dos.flush();                       }                         dos.close();  //it should close avoid continue deploy resource under view                   }                  }                if (flag==false)              {                   sock = new socket("10.0.2.2", 5991);                    flag = true;                 }         }           }       catch (unknownhostexception e)      {         e.printstacktrace();     }     catch (ioexception e)     {         e.printstacktrace();     } 

your read loop incorrect. need constrain read length don't over-run next file:

while (len > 0 && (smblen = clientdata,read(buffer, 0, len > buffer.length ? buffer.length : (int)len)) > 0) {     bos.write(buffer, 0, smblen);     len -= smblen; } 

other comments:

  • file lengths longs, not ints.
  • use bigger buffer, @ least 8192, , declare once @ top of method. don't need new 1 per file.
  • don't flush inside loop.
  • don't keep recreating streams. use same ones life of socket, @ both ends.
  • you should writing 'bos', not 'dos'. in fact don't need dataoutputstream write file @ all. bufferedoutputstream , fileoutputstream.
  • you should send 1 filename, 1 length, 1 file, next filename, ... way sender can stop time. gets rid of initial count, , gets rid of 'flag' nonsense. if eofexception reading next name, peer has closed connection.

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -