Linux网络发送和接收内核缓冲区大小的设置

socket属性:

  • SO_SNDBUF 发送缓冲区
SO_SNDBUF
Sets  or  gets  the  maximum socket send buffer in bytes. 
The kernel doubles this value (to allow space for bookkeeping overhead) when it 
is set using setsockopt(2), and this doubled value is returned by getsockopt(2). 
The default value is set by the /proc/sys/net/core/wmem_default file and the maximum 
allowed value is set by the /proc/sys/net/core/wmem_max file.
The minimum (doubled) value for this option is 2048.

在这里插入图片描述

  • SO_RCVBUF 接收缓冲区
SO_RCVBUF
Sets or gets the maximum socket receive buffer in bytes.  
The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this  doubled value  is  returned  by  getsockopt(2).   
The default value is set by the /proc/sys/net/core/rmem_default file, and the maximum 
allowed value is set by the /proc/sys/net/core/rmem_max file.  
The minimum (doubled) value for this option is 256.

在这里插入图片描述

设置socket的缓冲区大小:

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int getsockopt(int sockfd, int level, int optname,
                      void *optval, socklen_t *optlen);
       int setsockopt(int sockfd, int level, int optname,
                      const void *optval, socklen_t optlen);
  1. sockfd 文件描述符
  2. level 属性所属协议层,如IPPROTO_TCP和SOL_SOCKET
  3. optname 属性名
  4. optval 属性值
  5. optlen 属性值的长度

demo:

int rcv_buff_size = 10240;
int snd_buff_size = 8000;
int optlen = sizeof(rcv_buff_size);

setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&rcv_buff_size,optlen);

setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&snd_buff_size,optlen);

设置成功后,内核实际的缓冲区大小是我们设置的2倍,如上述代码,接收缓冲区大小为20480,发送缓冲区大小为8000.

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值