3

I have an application using an FTP library to transfer files. Receiving files works good, and mostly sending files also. The problem occurs when I'm trying to send a large file (3 MB), in which the application hangs. No error message, no nothing. I have debugged and found out that it stops in the socketWrite0() method in java.net.SocketOutputStream. This method never returns, but just hangs/blocks. It is called from:
java.io.BufferedOutputStream write() which calls
java.io.BufferedOutputStream flushBuffer() which calls
java.net.SocketOutputStream write() which calls
java.net.SocketOutputStream socketWrite() which calls
java.net.SocketOutputStream socketWrite0()

The file starts getting transferred, but the second time java.io.BufferedOutputStream flushBuffer() is called the transfer thread stops and hangs when it gets to the socketWrite0() method. I have tried adjusting the buffer size in BufferedOutputStream by sending a parameter in it's constructor, and it seems like it does not matter what this size is, it always stops on the second flush.

Then I tried to set the Socket setSendBufferSize to 3 000 000, and suddenly everything worked as it should, with the BufferedOutputStream size being default. As long as I set this buffer size to an amount larger than the file size the file is transferred and everything is good! If I set the buffer size to i.e. 400 000 the transfer stops after this amount has been transferred.

I cannot increase the BufferedOutputStream buffer unlimited. If I set this too big, the same thing happens: the application hangs at socketWrite0().

  1. Can anyone explain why?
  2. How large can the Socket buffer size be? What is the limit?
  3. How are the BufferedOutputStream buffer and the Socket buffer related?

Thank you very much!

Hippo
  • 115
  • 1
  • 2
  • 9
  • possible duplicate of [C++ socket programming Max size of tcp/ip socket Buffer?](http://stackoverflow.com/questions/12931528/c-socket-programming-max-size-of-tcp-ip-socket-buffer) – user207421 Oct 23 '12 at 09:51
  • According to this thread they say that "You can set these socket buffers as large as you like up to 2^32-1 bytes". So setting my socket send buffer to 3MB should be perfectly fine then? This is not too big...? – Hippo Oct 23 '12 at 09:58
  • That's what I said in that thread. You want me to repeat here it for some reason? – user207421 Oct 23 '12 at 11:44
  • No, just thought it seemed like a lot with 3MB, but I guess it's not. – Hippo Oct 23 '12 at 12:39

1 Answers1

0

Sounds like the other side is not reading from the socket so the socket is blocking, if you wrote the other side (the side that is supposed to be reading from the socket) then the problem is likely in that code. If not and you are trying to implement the FTP standard and talk to a known working FTP server then its more likely a problem with how you are implementing it on the client side (e.g. you are trying to write the file data but the remote side is not expecting it and is expecting a control message or something).

AntonyM
  • 1,602
  • 12
  • 12
  • The other side is a known working FTP server. I did not implement it, and it works fine with i.e. FileZilla. So the problem must be in my code / the library I'm using. I'll have a closer look into the data thread vs control thread and see if I can find anything suspicious. – Hippo Oct 25 '12 at 11:21