1

Init I have implemented a very basic port forwarding application, which takes inputs from the client and post it to a remote sftp server.

Sockets:

socket sock = server_sock.accept()
Socket client_sock = new Socket("remote sfpt server host",port);
client_sock.connect();

And input/output streams from each of these sockets are then copied using the following logic.

Copy:

byte b[] = new byte[65535];
int bytesRead = 0;
while ((bytesRead =in.read(b)) >= 0){
    out.write(b, 0, bytesRead);
    out.flush();
}

Problem

  1. Both sftp server and the basic port forwarding server are hosted on the same machine.

  2. Directly connecting to the sftp server, I can transfer a file at around 30MB/s, whereas

  3. Transferring a file via the port forwarding server is always pegged at ~3MB/s-4MB/s.

  4. I have tried with ServerSocketChannel as well but performance was same.

Question is how can I improve this basic port forwarding server's performance at per the sftp server?

vionixt
  • 121
  • 5
  • 1
    Hmm, a 64k buffer probably isn't going to help; network MTU is around 1500 bytes max. How about your [Receive Window size](https://en.wikipedia.org/wiki/TCP_tuning#Window_size) and your OS buffers, what's the max receive window compared to the sftp server? – markspace Jul 17 '15 at 15:25
  • You used a huge buffer, don't – HungPV Jul 17 '15 at 15:33
  • Tried till 8MB of buffer, no improvement at all. plus, tried setting setReceiveBufferSize for server[before binding] as well. – vionixt Jul 17 '15 at 15:37
  • I suggest you use blocking NIO with an off heap/direct buffer. You should be able to pass data at a rate of GB/s over loopback this way. – Peter Lawrey Jul 17 '15 at 16:23
  • 1
    Read while you write, don't block writing when reading. I have posted a forwarder with good performance before, you can find it [here](http://stackoverflow.com/a/23689356/3080094). – vanOekel Jul 17 '15 at 16:35

0 Answers0