0

Make the client and server to transfer multiple files over the network. Server on Windows, client Android. Faced with the problem of packet loss (if the server is Java), and file (if the serv in c++) probably because of asynchronous transfer. Found where the required delay (if the server is running on java, then after each batch, and if the serv in c++ after the transfer of each file), BUT the lower the speed, the greater the delay required. Maybe I something doing wrong? the answer here Java multiple file transfer over socket does not help

Client

    Socket socket = new Socket(ip, Integer.parseInt(portC));
       PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
       out.println("name.zip");
       Log.d("Out", "n-OK");

       byte[] bytes = new byte[1024];
       InputStream inf = new BufferedInputStream(new FileInputStream(file));
       OutputStream outf = new BufferedOutputStream(socket.getOutputStream());

       int count;
           int totalRead = 0;
       while ((count = inf.read(bytes)) != -1) {
            outf.write(bytes, 0, count);

               totalRead += count;
               System.out.println("\nread:"+count+"\ntotalread: "+totalRead);
 //delay if java server
 //try { Thread.sleep(100); } catch (InterruptedException err) { err.printStackTrace(); }
       }

       outf.flush();
       inf.close();
       outf.close();
       out.close();
       socket.close();
           lf.delete();
       Log.d("Out", "f-OK");

 //delay if с++ server
 //try { Thread.sleep(500); } catch (InterruptedException err) { err.printStackTrace(); }

Server

    String file = fl + "/" + name;
     try {
          InputStream inf = new BufferedInputStream(mySocket.getInputStream());
          OutputStream outf = new BufferedOutputStream(new FileOutputStream(file));

          int count;
          byte[] bytes = new byte[1024];

          while (size > 0 && (count = inf.read(bytes, 0, (int)Math.min(bytes.length, size))) != -1) {  
               outf.write(bytes, 0, count);   
            size -= count;    
            System.out.println("\nread:"+count+"\ntotalread: "+size);
        } 

        outf.flush();
        inf.close();
        outf.close();

        System.out.println("received file");
          dataSinh = "received file";

     } catch(Exception e)
    {
          System.out.println("received error");
          dataSinh = "received error";
          e.printStackTrace();
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexandr
  • 9
  • 3
  • `Beforehand all thank you ;)`??? No no no. Not in advance. – greenapps Mar 15 '17 at 17:15
  • You don't need any delay. The problem is qute different: the `BufferedWriter` and, presumably, the `BufferedReader` you must be using in the server to read the filename. See my answer in the duplicate for how to do this properly. – user207421 Mar 15 '17 at 17:18
  • the answer here http://stackoverflow.com/questions/10367698/java-multiple-file-transfer-over-socket does not help – Alexandr Mar 16 '17 at 07:11

0 Answers0