Is there a way to find out in Java the maximum allowed underlying read TCP socket buffer configured in the OS?

时间:2016-10-19 13:37:25

标签: java sockets networking tcp nio

I know you can call socket.setReceiveBufferSize(int) and socket.getReceiveBufferSize().

You can do something like:

socket.setReceiveBufferSize(Integer.MAX_VALUE);

int after = socket.getReceiveBufferSize();

if (after != Integer.MAX_VALUE) System.out.println("That did not work!");

Based on that, I would like to write an algo to guess correctly the max underlying read socket buffer size configured at the OS level. How can I do that? Binary search? Or is there a much simpler way?

1 个答案:

答案 0 :(得分:0)

after应该是最大尺寸,假设它实际上没有给你你想要的尺寸。使缓冲区太大可能会损害性能(以及浪费内存)

我会尝试将其设置为您希望的最大值,例如几MB,如果那也可能是最大值。