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();
}