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

查看:192
本文介绍了套接字: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());

我读到将8 KiB写入OutputStream时性能会受到影响,建议它将以小块写入,而不是一次写入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的传输速度最高,同时消耗的CPU比选项B多得多。选项B可能更好,因为它不能

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.

(套接字为 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天全站免登陆