0

I am a beginner in android development i have a question on how can i modify my code to send multiple files over socket, With my current code i can only send one file at a time, I have an ArrayList which is obtained from other Activity which contains Full Path of the file to be send but when i add more than one file to array it wont send anything. It is disturbing me from a whole month Any help or suggestions will be really appreciated

Here is my Sender's Code

@Override
public void run() {

Intent intent = getIntent();
Bundle bundle = intent.getExtras();  //Receiving Intent from other Activity
ArrayList send = (ArrayList) bundle.getParcelableArrayList("send"); //Full path of file in ArrayList
ArrayList name = (ArrayList) bundle.getParcelableArrayList("name"); //Name of the file if Available 
String con = send.toString();
String convert = con.replaceAll("\\[", "").replaceAll("\\]","");
file = new File(convert);

byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
   bis = new BufferedInputStream(new FileInputStream(file));
   DataInputStream dis = new DataInputStream(bis);
   OutputStream os = socket.getOutputStream();
   DataOutputStream dos = new DataOutputStream(os);
   if (name == null){
      dos.writeUTF(file.getName());
   }else {
      String conname = name.toString();
      String convertname = conname.replaceAll("\\[", "").replaceAll("\\]","");
      dos.writeUTF(convertname + ".apk");
  }

  int read;
  while ((read = dis.read(bytes)) != -1){
        dos.write(bytes,0,read);
  }

  socket.close();
}

Here is the receiver's

@Override
public void run() {

Socket socket = null;
int bytesRead;
InputStream in;
int bufferSize = 0;

try {
    socket = new Socket(dstAddress, dstPort);
    bufferSize = socket.getReceiveBufferSize();
    in = socket.getInputStream();
    DataInputStream clientData = new DataInputStream(in);
    String fileName = clientData.readUTF();
    File file = new File(Environment.getExternalStorageDirectory()  + fileName);  //Name and place of file to be save
    OutputStream output = new FileOutputStream(file);
    byte[] buffer = new byte[bufferSize];
    int read;
    while ((read = clientData.read(buffer)) != -1) {
          output.write(buffer, 0, read);
    }

    socket.close();

}

New Sender's code


            public void run() {

            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            ArrayList send = (ArrayList) bundle.getParcelableArrayList("send");
            ArrayList name = (ArrayList) bundle.getParcelableArrayList("name");

            BufferedInputStream bis;
            byte[] bytes = new byte[8192];

            try {
                //bis = new BufferedInputStream(new FileInputStream(file));
                OutputStream os = socket.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                for (int i =0; i<send.size();i++){
                    String filePath = send.get(i).toString();
                    File file = new File(filePath);

                    if (name!=null){ //check if name Array is empty or not
                        dos.writeUTF(String.valueOf(name.get(i)));
                    }else {
                        dos.writeUTF(file.getName());
                    }
                    dos.writeInt(file.size());
                };


                FileInputStream fis = new FileInputStream(file);
                DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));

                int read;
                while ((read = dis.read(bytes)) != -1){
                    dos.write(bytes,0,read);
                }

                fis.close();
                dos.flush();
                os.flush();
                socket.close();


New Receiver's code

             try {
                socket = new Socket(dstAddress, dstPort);
                bufferSize = socket.getReceiveBufferSize();
                in = socket.getInputStream();
                DataInputStream clientData = new DataInputStream(in);

                String fileName = clientData.readUTF();
                int filelength = clientData.readInt();

                File file = new File(Environment.getExternalStorageDirectory()  + fileName);  //Name and place of file to be save
                OutputStream output = new FileOutputStream(file);

                byte[] buffer = new byte[8192];
                int read;

                while ((read = clientData.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }

                socket.close();
              }

  • i did just like you said but it doesn't seem to work maybe i am doing it a bit wrong. can you provide me an example or similar thing/ project so that i can understand it and find the mistake? Thanks – Divyansh Kumar Aug 13 '20 at 10:34
  • @blackapps you there? – Divyansh Kumar Aug 14 '20 at 12:10
  • `dos.writeInt(send.size());` You should send the size of the file. Not of that array. How many bytes the receiver has to read for this file. `dos.writeInt(file.size());` – blackapps Aug 14 '20 at 12:15
  • `DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));` Remove that line. Edit it to: ` DataInputStream dis = new DataInputStream(new BufferedInputStream(fis)); `and place before the line `int read;` – blackapps Aug 14 '20 at 12:19
  • Add a line `FileInputStream fis = new FileInputStream(file);` And when you are done with that file a `fis.close();`. Update your code block please. – blackapps Aug 14 '20 at 12:20
  • so shouldn't be `dos.writeInt(file.size());` in the for loop? and in the for loop there is error with .size().... Should it be `dos.writeInt(file.length());` for the file size? **OVERWRITTINGLASTPOST** – Divyansh Kumar Aug 14 '20 at 16:07
  • Ok. file.length or whatever. You close the for loop after dos.writeInt(...);. No good. Close it after fis.close(); The for loop handles one file. That is the idea. – blackapps Aug 14 '20 at 19:18
  • After the for loop add a line `dos.writeUTF("end");`. – blackapps Aug 14 '20 at 19:22
  • Please adapt the sender code. After that post a new code block for the receiving side. – blackapps Aug 14 '20 at 19:28
  • yeah ok, i think we will use something like `while(clientData.readUTF() != "end"){ run loop}` am i right? **UPDATINGRECEIVERPOST** – Divyansh Kumar Aug 15 '20 at 04:38
  • @DivyanshKumar No. `readUTF()` doesn't return null. See my answer in the duplicate for how to do all this. – user207421 Aug 15 '20 at 05:25
  • yeah i saw your answer, but it doesn't seem to work most probably maybe i did something wrong but don't know what.... very Confused in my code – Divyansh Kumar Aug 15 '20 at 05:56
  • That code has been working for me since 1997. – user207421 Aug 15 '20 at 06:03
  • hello, @MarquisofLorne thanks for your point.... i considered redoing that post with your answer with some of my modification and it is successfully working now thanks for your help – Divyansh Kumar Aug 15 '20 at 06:45
  • and yeah @blackapps thank you too... you helped me in a lot of way – Divyansh Kumar Aug 15 '20 at 06:47

0 Answers0