查询最大的套接字发送缓冲区大小用C?

问题描述:

我知道我可以执行cat / proc / SYS /网/核心/ wmem_max以获取SO_SNDBUF套接字上的最大尺寸,但有一个简单的方法来查询C,它的价值不通过的缺憾感觉的步骤去打开文件,读取和转换为整数?

I know I can cat /proc/sys/net/core/wmem_max to get the maximum size for SO_SNDBUF on a socket, but is there an easy way to query that value in C without going through the kludgy-feeling steps of opening the file, reading, and converting to an integer?

要获得net.ipv4.tcp_wmem的sysctl的价值,你需要解析出来的/ proc文件重新presenting sysctl变量是的(有真的是在Linux上没有更好的办法,并且将sysctl系统调用早已被撤销pcated $ p $)。

To get the value of the net.ipv4.tcp_wmem sysctl, you need to parse it out of the /proc file representing that sysctl (there really is no better way on Linux, and the sysctl system call has long since been deprecated.)

是这样的:

unsigned long wmem_min,wmem_default,wmem_max;
FILE *f = fopen("/proc/sys/net/ipv4/tcp_wmem", "r");
if(f == NULL)
   fail();
if(fscanf(f, "%lu %lu %lu", &wmem_min,&wmem_default,&wmem_max) != 3)
  fail();

fclose(f);
 ... use wmem_max

有关特定的插座,您可以得到当前剩余的缓冲区

For a particular socket, you can get the current remaining buffer with

  socklen_t optlen;
  int send_buf, rc;
  optlen = sizeof(send_buf);
  //if getsockopt is successful, send_buf will hold the buffer size
  rc = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_buf, &optlen);