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

查看:151
本文介绍了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();

我试图找到刷新在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算法,几乎可以肯定没有,但是您不能百分百确定.

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天全站免登陆