2

如果我发送 1000 个“Hello World!” UDP 消息(12 个字节 + 28 个 IP/UDP 开销),我观察到在接收端我只缓冲 658 个(总是相同的数字,658*40 = 26320 个字节)。我这样做是通过在服务器上睡觉时发送 UDP 消息(在创建套接字之后)。

奇怪的是服务器上的 SO_RCVBUF 选项是 42080 字节。所以,我想知道为什么我不能缓冲 1000 条消息。你知道剩下的 15760 字节花在哪里了吗?

在服务器代码下方(其中 distrib.h 包含套接字和信号处理函数的基本错误处理包装器):

#include "distrib.h"

static int count;
static void sigint_handler(int s) {
  printf("\n%d UDP messages received\n",count);
  exit(0);
}

int main(int argc, char **argv) 
{
  struct addrinfo* serverinfo;
  struct addrinfo hints;
  struct sockaddr_storage sender;
  socklen_t len;
  int listenfd,n;
  char buf[MAXLINE+1];

  if (argc != 2) {
    log_error("usage: %s <port>\n", argv[0]);
    exit(1);
  }

  Signal(SIGINT,sigint_handler);

  bzero(&hints,sizeof(hints));
  hints.ai_family = AF_INET;    
  hints.ai_socktype = SOCK_DGRAM; 
  hints.ai_protocol = IPPROTO_UDP;
  Getaddrinfo("127.0.0.1", argv[1], &hints, &serverinfo);

  listenfd = Socket(serverinfo->ai_family, serverinfo->ai_socktype, 
            serverinfo->ai_protocol);
  Bind(listenfd, serverinfo->ai_addr,serverinfo->ai_addrlen);
  freeaddrinfo(serverinfo);

  count =0;
  sleep(20); 
  while(true) {
    bzero(buf,sizeof(buf));
    len = sizeof(sender);
    n = Recvfrom(listenfd, buf, MAXLINE, 0, (struct sockaddr*)&sender,&len);
    buf[n]='\0';
    count++;

  }

  close(listenfd);

  return 0;
}
4

2 回答 2

5

进行反向计算会提供更多信息——你的缓冲区是 42080,它在开始丢弃之前缓冲了 658 个数据包。现在 42080/658 = 63.95,所以看起来它会将每个数据包计数为 64 个字节,如果到目前为止缓冲的数据包的总大小等于或高于限制,则丢弃数据包。由于它缓冲了整个数据包,因此它实际上最终缓冲的数量略多于限制。

为什么是 64 字节而不是 40 字节?也许它包括一些排队开销,或者它可能是为了对齐而四舍五入到 2 的某个幂的倍数,或者可能是两者的某种组合。

于 2013-05-08T15:19:02.553 回答
2

I dont have a complete answer, but I tested this on my Linux box and this is what I observed.

When I send one "Hello World!\n" with a terminating '0'. I get:

Client:

$./sendto
sent 14 bytes

Socket "Recv-Q" has 768 bytes (seems probable its in bytes, did not check ss sources):

$ ss -ul|grep 55555
UNCONN     768    0               127.0.0.1:55555                    *:*     

When I send 1000 packets I get:

$ ./sendto 
sent 14000 bytes

Recv-Q:

$ ss -ul|grep 55555
UNCONN     213504 0               127.0.0.1:55555                    *:*       

Your server (after ctrl-c):

$ ./recvfrom 55555
^C
278 UDP messages received

Incidentally 213504/768 = 278. With quick experimentation I could not figure out what setting to tune, to increase the buffered amount. Also, I dont know why a received packet takes so much space in this queue. Lots of metadata maybe? As on you osX, the dropped packets show up in netstat -su.

EDIT: Additional observation with ss -ulm, which prints "socket memory usage" in more detail:

UNCONN     213504 0               127.0.0.1:55555                    *:*       
skmem:(r213504,rb212992,t0,tb212992,f3584,w0,o0,bl0)

The 213504 bytes buffered are 512 bytes above the rb value. Might not be a coincidence, but would require reading the kernel source to find out.

Did you check how much one UDP datagram takes up on osX?

EDIT 2: This is still not a suitable answer for osX, but on Linux I found that increasing the kernel memory allocated for receive buffers allowed me to buffer all the 1000 packets sent.

A bit of overkill, but I used these (disclaimer) tweaking the buffer values randomly might seriously mess up your networking and kernel):

net.core.rmem_max=1048568
net.core.rmem_default=1048568
于 2013-05-08T14:46:59.110 回答