SO_SNDBUF 和 SO_RCVBUF 是什么 [英] What are SO_SNDBUF and SO_RCVBUF

查看:31
本文介绍了SO_SNDBUF 和 SO_RCVBUF 是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下 SO_SNDBUFSO_RCVBUF 选项到底是什么吗?

Can you explain me what exactly are SO_SNDBUF and SO_RCVBUF options?

好的,出于某种原因,操作系统会缓冲传出/传入的数据,但我想澄清一下这个问题.

OK, for some reason the OS buffers the outgoing/incomming data but I'd like to clarify this subject.

  • 他们的角色(通​​常)是什么?
  • 它们是每个套接字的缓冲区吗?
  • 传输层的缓冲区(例如 TCP 缓冲区)与这些缓冲区之间是否存在连接?
  • 在使用流式套接字 (TCP) 和使用无连接套接字 (UDP) 时,它们的行为/作用是否不同?

一篇好文章也会很棒.

我搜索了它,但没有找到任何有用的信息.

I googled it but didn't find any useful information.

推荐答案

SO_"前缀用于socket option",所以是的,这些是每个套接字缓冲区的每个套接字设置.通常有系统范围的默认值和最大值.

The "SO_" prefix is for "socket option", so yes, these are per-socket settings for the per-socket buffers. There are usually system-wide defaults and maximum values.

SO_RCVBUF 更容易理解:它是内核分配的缓冲区大小,用于保存到达给定套接字的数据,在数据通过网络到达和被拥有这个套接字的程序.使用 TCP,如果数据到达而您没有读取它,缓冲区将填满,发送方将被告知放慢速度(使用 TCP 窗口调整机制).对于 UDP,一旦缓冲区满了,新的数据包就会被丢弃.

SO_RCVBUF is simpler to understand: it is the size of the buffer the kernel allocates to hold the data arriving into the given socket during the time between it arrives over the network and when it is read by the program that owns this socket. With TCP, if data arrives and you aren't reading it, the buffer will fill up, and the sender will be told to slow down (using TCP window adjustment mechanism). For UDP, once the buffer is full, new packets will just be discarded.

SO_SNDBUF,我认为,只对 TCP 重要(在 UDP 中,您发送的任何内容都会直接发送到网络).对于 TCP,如果远程端没有读取,您可以填充缓冲区(以便远程缓冲区变满,然后 TCP 将此事实传达给您的内核,并且您的内核停止发送数据,而是将其累积在本地缓冲区中,直到它填上).或者如果存在网络问题,它可能会填满,并且内核没有收到它发送的数据的确认.然后它将减慢在网络上发送数据的速度,直到最终传出缓冲区填满.如果是这样,应用程序对这个套接字的未来 write() 调用将阻塞(或者如果您设置了 O_NONBLOCK 选项,则返回 EAGAIN).

SO_SNDBUF, I think, only matters for TCP (in UDP, whatever you send goes directly out to the network). For TCP, you could fill the buffer either if the remote side isn't reading (so that remote buffer becomes full, then TCP communicates this fact to your kernel, and your kernel stops sending data, instead accumulating it in the local buffer until it fills up). Or it could fill up if there is a network problem, and the kernel isn't getting acknowledgements for the data it sends. It will then slow down sending data on the network until, eventually, the outgoing buffer fills up. If so, future write() calls to this socket by the application will block (or return EAGAIN if you've set the O_NONBLOCK option).

这一切在 Unix 网络编程

This all is best described in the Unix Network Programming book.

这篇关于SO_SNDBUF 和 SO_RCVBUF 是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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