java接口转发 不同端口,在Java中快速实现端口转发

I have build a simple application that opens a ServerSocket, and on connection, it connects itself to another server socket on a remote machine. To implement port forwarding, I use two threads, one that reads from the local inputstream and streams to the remote sockets outputstream, and vice versa.

The implementation feels a bit inperformant, and so I ask you if you know a better implementation strategy, or even have some code lying around to achive this in a performant way.

PS: I know I could use IPTables on Linux, but this has to work on Windows.

PPS: If you post implementations for this simple task, I will create a benchmark to test all given implementations. The solution should be fast for many small (~100bytes) packages and steady data streams.

My current implementation is this (executed on each of the two threads for each direction):

public static void route(InputStream inputStream, OutputStream outputStream) throws IOException {

byte[] buffer = new byte[65536];

while( true ) {

// Read one byte to block

int b = inputStream.read();

if( b == - 1 ) {

log.info("No data available anymore. Closing stream.");

inputStream.close();

outputStream.close();

return;

}

buffer[0] = (byte)b;

// Read remaining available bytes

b = inputStream.read(buffer, 1, Math.min(inputStream.available(), 65535));

if( b == - 1 ) {

log.info("No data available anymore. Closing stream.");

inputStream.close();

outputStream.close();

return;

}

outputStream.write(buffer, 0, b+1);

}

}

解决方案

A couple of observations:

The one byte read at the start of the loop does nothing to improve performance. Probably the reverse in fact.

The call to inputStream.available() is unnecessary. You should just try to read to "buffer size" characters. A read on a Socket streamwill return as many characters as are currently available, but won't block until the buffer is full. (I cannot find anything in the javadocs that says this, but I'm sure it is the case. A lot of things would perform poorly ... or break ... if read blocked until the buffer was full.)

As @user479257 points out, you should get better throughput by using java.nio and reading and writing ByteBuffers. This will cut down on the amount of data copying that occurs in the JVM.

Your method will leak Socket Streams if a read, write or close operation throws an exception. You should use a try ... finally as follows to ensure that the streams are always closed no matter what happens.

public static void route(InputStream inputStream, OutputStream outputStream)

throws IOException {

byte[] buffer = new byte[65536];

try {

while( true ) {

...

b = inputStream.read(...);

if( b == - 1 ) {

log.info("No data available anymore. Closing stream.");

return;

}

outputStream.write(buffer, 0, b+1);

}

} finally {

try { inputStream.close();} catch (IOException ex) { /* ignore */ }

try { outputStream.close();} catch (IOException ex) { /* ignore */ }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值