0

I am sending files between 2 devices, so I established a socket communication. Right now, I am just trying to send one file, but in the future I want to send multiple files (selected by the user from a gridview).

The problem is that when I send one file, on the server side (that receives the file) the socket.getInputStream().read(buffer) does not detect the end of the file. It just waits for "more" data to be sent.

After searching a bit on this issue, I reached some topics that kind of gave me some options, but I am still not satisfied with it because I dont know if those options would be efficient to send multiple files. This is an example : How to identify end of InputStream in java

I could close the socket or the stream objects after sending a file, but if I want to send a lot of files, it wouldn't be efficient to be always closing and opening the sockets.

Code on the receiver :

  File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/testeReceiveServerComm.apk");
  byte[] buffer = new byte [8192];
  FileOutputStream fos=new FileOutputStream(apkReceived);
  int count=0;
  int total=0;//so apra ir vendo quanto recebi.
  while((count = in.read(buffer)) != -1){
        fos.write(buffer,0,count);
        total+=count;
        System.out.println("Server Comm receive thread - already received this ammount : "+total);

  }

Code on the client (sender) :

  File apkToSend=new File(filePath);
  byte[] buffer = new byte [8192];
  BufferedInputStream bis=new BufferedInputStream(new FileInputStream(apkToSend));
  int count;
  int total=0; 
  while((count=bis.read(buffer))!=-1){
         out.write(buffer,0,count);
         total+=count;
         out.reset();
         System.out.println("send thread - already sent this ammount : "+total);
            }

            out.flush();
            bis.close();
Community
  • 1
  • 1
  • Do not rely on end of stream. Its much simpler. Just send the size of the file first. You can put the size in an integer variable and then send the four bytes of the integer. The receiver first reads four bytes and knows the size. Then it is going to read exactly size bytes from the stream. When done it reads again four bytes for the size of the next file. When all four bytes are zero there are no more files. – greenapps Aug 16 '16 at 12:07
  • the easiest is to use for example [protocol buffers](https://developers.google.com/protocol-buffers/) to 1) write a file "descriptor" (path, filename, description, filesize etc) and based of `filesize` write that exact number of bytes, then go to 1), the reader does the same thing on its input stream – pskink Aug 16 '16 at 12:17
  • Thank you to both. Greenapps approach is similar to the answer that Riyaz Parasara wrote, so i implemented something similar. Psink thank you a lot for your answer , will definitely check the protocol buffers, maybe it would be more efficient (i would only read 1 time in order to have the size and the file?). Again, thank you both for your answers – Filipe Gonçalves Aug 16 '16 at 12:22

0 Answers0