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);
- sockfd 文件描述符
- level 属性所属协议层,如IPPROTO_TCP和SOL_SOCKET
- optname 属性名
- optval 属性值
- 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.