(10) 如何获取 linux 系统上的 TCP 、 UDP 套接字的收发缓存的默认大小,以及代码范例

(1) 先介绍下后面的代码里要用到的基础函数:

++ 以及:

++

(2) 接着给出现代版的 读写 socket 参数的系统函数 :

++ 以及:

(3) 给出 一言的 范例代码,获取当代 linux 系统的 tcp 套接字的缓存大小:

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>

int main() 
{
    int sockfd , sndbuf, rcvbuf ;        socklen_t optlen;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);  // 创建一个 TCP 套接字
    if (sockfd < 0) {   perror("socket");    exit(EXIT_FAILURE);  }

    // 获取发送缓存大小
    optlen = sizeof(sndbuf);
    if (getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (void *)&sndbuf, &optlen) < 0) {
        perror("getsockopt SO_SNDBUF");
        close(sockfd);       exit(EXIT_FAILURE);
    }
    printf("Send buffer size: %d bytes\n", sndbuf);

    // 获取接收缓存大小
    optlen = sizeof(rcvbuf);
    if (getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, &optlen) < 0) {
        perror("getsockopt SO_RCVBUF");
        close(sockfd);        exit(EXIT_FAILURE);
    }
    printf("Receive buffer size: %d bytes\n", rcvbuf);

    close(sockfd);  // 关闭套接字

    return 0;
}

++ 给出 ubantu 的打印结果( 跟 30 年前的 伯克利系统里的缓存设置,已经有了很大的差别)

++ 以下是 30 年前的 tcp/ip 协议的系统缓存设置:

(4)

谢谢

相关推荐
m0_74215543几秒前
linux进程通讯-使用消息队列完成子父进程间的通讯
linux·c++
小林熬夜学编程2 分钟前
【MySQL】第一弹---MySQL 在 Centos 7环境安装
linux·开发语言·数据库·mysql·算法
hunter20620621 分钟前
联想拯救者开机进入bios
linux
A-刘晨阳22 分钟前
Linux生成自签证书【Nginx】
linux·运维·nginx·ssl
m0_7482482325 分钟前
Java进阶14 TCP&日志&枚举
java·开发语言·tcp/ip
唐古乌梁海1 小时前
【centOS】安装docker环境,替换国内镜像
linux·docker·centos
hgdlip2 小时前
ip属地是实时刷新吗还是网络刷新
服务器·网络·tcp/ip
修炼成精2 小时前
.net framework 4.5 的项目,用Mono 部署在linux
linux·运维·.net
慕雪华年2 小时前
【VM】VirtualBox安装ubuntu22.04虚拟机
linux·ubuntu