有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用socket。setReceiveBufferSize()

我对java中Socket的setReceiveBufferSize()的使用感到困惑。净

通过API,我知道为socket设置接收缓冲区大小定义(或给出提示)socket一次可以接收的数据限制。然而,每次我尝试读取socket的输入流时,我发现它可以存储比我用setReceiveBufferSize()设置的更多的内容

考虑下面的代码:

InputStream input_stream = socket.getInputStream();
socket.setReceiveBufferSize(1024);
byte[] byte_array = new byte[4096];

input_stream.read(byte_array);

每次我从input_stream读取数据时,我已经测试过,只要发送方已经发送了超过1024个字节的数据,我实际上一次可以读取超过1024个字节(并填充4096字节数组)

有人能解释一下为什么会发生这种情况吗?我是不是错过了什么?多谢各位


共 (1) 个答案

  1. # 1 楼答案

    From the API, I know that setting the receive buffer size for the socket defines (or gives a hint to) the data limit that the socket can receive at a time.

    不,它不会。它会向TCP提示接收缓冲区的总大小,这反过来会影响可播发的最大接收窗口。”“一次接收”与此无关

    However, every time I try to read from the socket's input stream, I've found out that it can store more than what I set with setReceiveBufferSize().

    TCP可以自由地向上或向下调整提示。在本例中,1024是一个可笑的小尺寸,任何实现都会增加到至少8192。您可以了解getReceiveBufferSize()实际使用了多少TCP