UDP¿ª·¢Ïà¹ØµÄһЩÐĵÃ
(2008-10-05 21:35)
·ÖÀࣺ UNIXÍøÂç±à³Ì
ºÃ¾Ãû½Ó´¥¹ý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.
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¶ªÆúÊý¾Ý±¨¡£
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 |
/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 |
/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-<version>/Documentation/networking/ip-sysctl.txt. There is a very good (but slightly out of date) tutorial on network sysctl's at http://ipsysctl-tutorial.frozentux.net/ipsysctl-tutorial.html.


