TCP_NODELAY 如何影响连续的 write() 调用? [英] How does TCP_NODELAY affect consecutive write() calls?

查看:21
本文介绍了TCP_NODELAY 如何影响连续的 write() 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TCP_NODELAY 是启用快速发送 TCP 数据包的选项,无论其大小如何.当速度很重要时,这是非常有用的选项,但是,我很好奇它会对此做什么:

TCP_NODELAY is an option to enable quick sending of TCP packets, regardless of their size. This is very useful option when speed matters, however, I'm curious what it will do to this:

Socket socket = [some socket];
socket.setTcpNoDelay(true);
OutputStream out = socket.getOutputStream();
out.write(byteArray1);
out.write(byteArray2);
out.write(byteArray3);
out.flush();

我试图找出flush在SocketOutputStream上实际做了什么,但据我现在所知,它没有做任何事情.我曾希望它会告诉套接字立即发送所有缓冲数据",但不幸的是,它没有.

I tried to find what flush actually does on a SocketOutputStream, but as far as I know now, it doesn't do anything. I had hoped it would tell the socket "send all your buffered data NOW", but, unfortunately, no it doesn't.

我的问题是:这些 3 字节数组是在一个数据包中发送的吗?我知道您对 TCP 如何构建网络数据包没有太多控制权,但是有什么方法可以告诉套接字(至少尝试)打包这些字节数组,从而避免网络开销?可以手动打包字节数组并在一次调用中将它们发送到 write 帮助吗?

My question is: are these 3 byte arrays sent in one packet? I know you don't have much control over how TCP constructs a network packet, but is there any way to tell the socket to (at least try to) pack these byte arrays, so network overhead is avoided? Could manually packing the byte arrays and sending them in one call to write help?

推荐答案

我的问题是:这些 3 字节数组是在一个数据包中发送的吗?

My question is: are these 3 byte arrays sent in one packet?

由于您禁用了 Nagle 算法,几乎可以肯定不会,但您不能 100% 确定.

As you have disabled the Nagle algorithm, almost certainly not, but you can't be 100% sure.

我知道您对 TCP 如何构建网络数据包没有太多控制权,但是有什么方法可以告诉套接字(至少尝试)打包这些字节数组

I know you don't have much control over how TCP constructs a network packet, but is there any way to tell the socket to (at least try to) pack these byte arrays

是的.不要禁用 Nagle 算法.

Yes. Don't disable the Nagle algorithm.

这样可以避免网络开销吗?可以手动打包字节数组并在一次调用中发送它们以编写帮助吗?

so network overhead is avoided? Could manually packing the byte arrays and sending them in one call to write help?

是的,或者更简单的仍然只是将套接字输出流包装在 BufferedOutputStream 中,并在您想要发送数据时调用 flush(),根据您当前的代码.flush() 对套接字输出流不执行任何操作是正确的,但它会刷新 BufferedOutputStream.

Yes, or simpler still just wrap the socket output stream in a BufferedOutputStream and call flush() when you want the data to be sent, as per your present code. You are correct that flush() does nothing on a socket output stream, but it flushes a BufferedOutputStream.

这篇关于TCP_NODELAY 如何影响连续的 write() 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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