套接字:BufferedOutputStream 还是只是 OutputStream? [英] Sockets: BufferedOutputStream or just OutputStream?

查看:28
本文介绍了套接字:BufferedOutputStream 还是只是 OutputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在 Java 中通过 TCP 获得最快的传输速度,这是更好的:

In order to get the fastest transfer speeds over TCP in Java, which is better:

选项 A:

InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();

选项 B:

BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

我读到当向 OutputStream 写入超过 8 KiB 时性能会受到影响,建议将其写入小块而不是一次 8 KiB.8 KiB 是 BufferedOutputStream 的默认缓冲区大小.

I've read that performance takes a hit when writing over 8 KiB to OutputStream, it was recommended that it'd be written to it in small chunks not pver 8 KiB at a time. 8 KiB is the default buffer size of a BufferedOutputStream.

不过,我也读到过,当通过网络传输数据时,最好尽快清除字节.这意味着使用缓冲区并以小块写入会增加不必要的开销.

However I've also read that when transfering data over the net it's good to flush out the bytes as quickly as possible. Meaning that using a buffer and writing in small chunks adds a unneeded overhead.

那么,选项A还是选项B?哪个效果最好?

So, option A or option B? Which works best?

现在我猜测选项 A 提供最高的传输速度,同时比选项 B 消耗更多的 CPU.选项 B 可能更好,因为它不会慢很多,但可以节省大量 CPU.

Right now I'm guessing option A gives highest transfer speeds while consuming a lot more CPU than option B. Option B might be better since it doesn't go that much slower but saves a lot of CPU.

--

额外问题:触摸 TCP 窗口大小是个好主意吗?例如将其设置为 64 KiB:

Bonus question: Is it a good idea to touch the TCP window size? For example by setting it to 64 KiB:

socket.setReceiveBufferSize(65536);
socket.setSendBufferSize(65536);

我尝试在测试机器上将其设置为 128 KiB,因为我读到它可以提高速度,但是当服务器获得几个连接时,CPU 处于 100% 而不是像我单独放置时那样的 ~2%.我想 128 KiB 太高了,除非你有一个很好的服务器来处理这种流量高峰,但是将它设置为 32 KiB 这样的东西是否明智?我认为那里的默认值是 8 KiB.

I tried setting it to 128 KiB on a test machine since I read that it could increase speeds but when the server got a couple of connections the CPU were at 100% instead of ~2% like when I left it alone. I guess 128 KiB is way too high unless you've got a good server that can handle that rush in traffic, but is it smart to set it to something like 32 KiB? I think the default was 8 KiB there for me.

("socket" 是 "java.net.Socket")

("socket" is "java.net.Socket")

推荐答案

我不知道你是从哪里读到的那些废话,但它完全回到了前面.一次写入 TCP 连接的次数越多越好,并且每次读取的内容也越多,同上.我总是在应用程序和套接字流之间使用缓冲流、读取器或写入器.在某些情况下,例如 SSL,一次直接写入一个字节会导致 40 倍的数据爆炸.

I don't know where you read all that nonsense but it is completely back to front. The more you write at a time to a TCP connection the better, and the more you read from it at a time ditto. I would always use a buffered stream, reader, or writer between the application and the socket streams. There are some cases like SSL where directly writing a byte at a time can cause a 40x data explosion.

触摸 TCP 窗口大小是个好主意吗?例如将其设置为 64 KiB

Is it a good idea to touch the TCP window size? For example by setting it to 64 KiB

您不能触摸 TCP 窗口大小".您可以通过您提到的 API 增加其最大值,这确实是一个好主意.

You can't 'touch the TCP window size'. You can increase its maximum value via the APIs you mention, and that is indeed a good idea.

这篇关于套接字:BufferedOutputStream 还是只是 OutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆