My question is straight forward. I made an app with java sockets which can send as many files as you want between two users.However, sometimes transfer can be slow and i think it is because i am sending like this:
DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
Instead of this:
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
I also added socket.setTcpNoDelay(true)
just in case to make sending faster.
I send byte[] array of size 8192 over the network but it will never get sent in it’s original size due to fragmentation and underlying network configuration so i pretty much send as much as OS can handle. Is it possible to make this faster in any way?
I also send byte arrays like this: (Maybe reading file in some other way than like below with FileInputStream might make things faster?)
File file = files.get(key);
byte[] buf = new byte[8192];
FileInputStream fis = new FileInputStream(file);
//read parts of a file and write them to dos
while ((n = fis.read(buf, 0, (int) Math.min(buf.length, fileSize))) != -1) {
dos.write(buf, 0, n);
dos.flush();
if (//if it is aborted while sending) {
dos.writeBoolean(true);//if it was aborted we will send true
dos.flush();
break;
}