Chinaunix首页 | 论坛 | 博客
  • 博客访问: 331219
  • 博文数量: 4
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 377
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-03 15:28
文章分类

全部博文(4)

文章存档

2008年(4)

我的朋友

分类: C/C++

2008-10-05 21:35:14

好久没接触过udp的底层开发了,需要做个udp server。由于client可能是固定端口发送过来的。不记得recvfrom是否可能一次接受多个package。翻了unp等书也没找到。就顺便研究了udp相关的一些问题。
 
1.关于rcvfrom是否可能获取多个package?
man udp
解答很明确如下:
All receive operations return only one  packet.   When  the  packet  is
smaller than the passed buffer only that much data is returned, when it
bigger the packet is  truncated  and  the  MSG_TRUNC  flag  is  set.
MSG_WAITALL is not supported.
也就是说一次只能接受一个package
 
2.  SO_RCVBUF和SO_SNDBUF
 
 每个套接口都有一个发送缓冲区和一个接收缓冲区。接收缓冲区被TCP和UDP用来将接收到的数据一直保存到由应用进程来读。

TCP:TCP通告另一端的窗口大小。TCP套接口接收缓冲区不可能溢出,因为对方不允许发出超过所通告窗口大小的数据。这就是TCP的流量控制,如果对方无视窗口大小而发出了超过宙口大小的数据,则接

收方TCP将丢弃它。

UDP:当接收到的数据报装不进套接口接收缓冲区时,此数据报就被丢弃。UDP是没有

流量控制的;快的发送者可以很容易地就淹没慢的接收者,导致接收方的UDP丢弃数据报。
 
max size的调试可以参考以下链接:
tcp具体有2个参数
 The per connection memory space defaults are set with two 3 element arrays:
1
2
	/proc/sys/net/ipv4/tcp_rmem       - memory reserved for TCP rcv buffers
	/proc/sys/net/ipv4/tcp_wmem       - memory reserved for TCP snd buffers


These are arrays of three values: minimum, initial and maximum buffer size. They are used to set the bounds on autotuning and balance memory usage while under memory stress. Note that these are controls on the actual memory usage (not just TCP window size) and include memory used by the socket data structures as well as memory wasted by short packets in large buffers. The maximum values have to be larger than the BDP of the path by some suitable overhead.
 
UDP是以下两个参数
The maximum buffer size that applications can request (the maximum acceptable values for SO_SNDBUF and SO_RCVBUF arguments to the setsockopt() system call) can be limited with /proc variables:
1
2
	/proc/sys/net/core/rmem_max       - maximum receive window
	/proc/sys/net/core/wmem_max       - maximum send window

The kernel sets the actual memory limit to twice the requested value (effectively doubling rmem_max and wmem_max) to provide for sufficient memory overhead. You do not need to adjust these unless your are planing to use some form of application tuning.
具体内核参数调试参看
For additional information on kernel variables, look at the documentation included with your kernel source, typically in some location such as /usr/src/linux-/Documentation/networking/ip-sysctl.txt. There is a very good (but slightly out of date) tutorial on network sysctl's at


 
阅读(2963) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~