0

I will try to break down my problem and explain what is going on as it is impossible to post my whole code here (thousands of lines).

I am implementing a client/server application and I am trying to send a file over a socket (SSLSocket). I have written below a block of my code.

The problem occurs when trying to send large files (trying to send a 100MB pdf). When I am sending txt files only a few kb long everything works great. As soon as I try to send a larger file it all falls apart. And by that I mean: the client stalls at the line where it is sending the file bytes[] and the server gets only part of the (starting) file bytes and ignores the rest continuing the execution. For example if the client sends the bytes [1,2,3,4,5,...,6,7,8,9,10] the server will get something like [1,2,3,4,5,...,0,0,0,0,0] (it is not actually receiving 0s after a point but as it is initialised with the correct size the initialised bytes stay to the starting value).

client

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

dos.write(largeByteArray);
dos.flush();

server

DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

dest = new byte[(int) sizeOfFileReceived]; 
dis.read(dest);

My guess was that the server fails to ACK the client because it is trying to receive all the bytes in one go and this is where it all falls apart.

I tried to change the code on the receiving side to get chunks of bytes (8000bytes) and then concatenate them into a single array but didn't work out well (might be a fault of my implementation though). I used the following posts to get ideas but might have done it wrong: here and here

Any ideas on what is going on?

EDIT: What i tried was something like this:

byte[] buf = new byte[8000];
int bytesRead;
int pos = 0;
while ((bytesRead = dis.read(buf, 0, buf.length)) != -1) {
    System.arraycopy(dest, pos, buf, 0, bytesRead);
    pos += bytesRead;
}

UPDATE: when using the code of the last edit the problem is that in the last call of the dis.read(buf, 0, buf.length) it completely stalls with no exception without returning any result instead of -1

Community
  • 1
  • 1
Rakim
  • 1,087
  • 9
  • 21
  • 40
  • Yes, you must use a loop to both send and receive the bytes, in chunks. You didn't say what didn't work out well. Why didn't it work out well? You have to show logs. The length in `arraycopy` will not always be the entire array length. The last bit will be less than 8000. – Chloe Nov 07 '15 at 23:35
  • I m not sure what didnt work out well so I didnt want to guess. It is really hard to debug because It is delaying the ACKs and everything stops working - I am trying it again now actually so might find out more. I haven't tried breaking in chunks the sending side. I thought it was fine like that. the `dest` was defined as `byte[] dest= new byte[(int) sizeOfFileSent]` – Rakim Nov 07 '15 at 23:36
  • Have you not considered using [Apache Commons library](https://commons.apache.org/proper/commons-fileupload/index.html) to handle this for you? – t0mm13b Nov 07 '15 at 23:36
  • @t0mm13b No I haven't but I don't want to use a library to do so. I need to have the minimum amount of dependencies possible – Rakim Nov 07 '15 at 23:38
  • That's the whole point of re-using existing dependencies, save you time and effort. Any reason why there's a need to keep dependencies minimum, please state that in the question. – t0mm13b Nov 07 '15 at 23:42
  • @t0mm13b because it is all for learning purposes. So no point using libraries to do everything. – Rakim Nov 07 '15 at 23:45
  • as a FYI, do you have the server configured to accept large files? maybe the server side is the issue? Have you investigated? – t0mm13b Nov 07 '15 at 23:53
  • @t0mm13b I am not sure what you mean. Configure it from where? – Rakim Nov 07 '15 at 23:56
  • If it blocks it means the peer isn't closing the continued. You don't *need* large byte arrays to send large files. An 8k buffer is sufficient. See my answer in the duplicate. – user207421 Nov 08 '15 at 00:09
  • @EJP on it atm. trying to work out my problem based on that – Rakim Nov 08 '15 at 00:15

0 Answers0