34. UDP协议


接下来准备简单探讨一下关于UDP套接字编程.


UDP头部

UDP是面向报文.

在这里插入图片描述
UDP的头部是固定8字节, 分别是 : 源端口, 目的端口, 长度, 检验和.

校验和是用来保证数据的安全.


UDP协议

UDP是一种无连接的传输层协议, 提供面向事务的简单不可靠信息传送服务, 适合于一次传输少量数据; 在网络环境较差的情况下, UDP协议数据包丢失会比较严重.

不可靠 :

  • 不提供报文到达确认
  • 数据排序
  • 流量控制
  • 拥塞控制

UDP的优势 :

  • 它不属于连接型协议, 因而具有资源消耗小, 处理速度快.

  • UDP 数据传输过程中延迟小、数据传输效率高传输效率很快. 当然可以在应用层对UDP实现可靠传输.

  • 吞吐量不受拥挤控制算法的调节

  • UDP使用尽最大努力交付.

既然UDP并不可靠, 那什么时候会用这种不可靠协议呢? UDP在DNS, TFTP, SNMP中都用到过.

前面在分析TCP缓冲区的时候也提到过. UDP没有发送缓冲区只有接收缓冲区, 如果对端的接收缓冲区满了则其他数据也将被直接丢弃.


TCP与UDP

UDP和TCP协议的主要区别是两者在如何实现信息的可靠传递方面不同.

TCP协议中包含了专门的传递保证机制, 当数据接收方收到发送方传来的信息时, 会自动向发送方发出确认消息; 发送方只有在接收到该确认消息之后才继续传送其它信息, 否则将一直等待直到收到确认信息为止.

UDP协议并不提供数据传送的保证机制. 如果在从发送方到接收方的传递过程中出现数据报的丢失, 协议本身并不能做出任何检测或提示.


同样TCP是字节流, 通信的发送端的写次数和接收端的读次数是没有次数之间的联系的; 而UDP是面向报文的, 通信的发送端的写次数和接收端的读次数是必须相同的, 如果数据没有读完也必须丢弃.


小结

简单的罗列了UDP协议的重要知识点, 接下来就要开始从编程的角度来验证和实现UDP通信了.

  • UDP不可靠, 无连接
  • 只有接收缓冲区
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
INTRODUCTION lwIP is a small independent implementation of the TCP/IP protocol suite. The focus of the lwIP TCP/IP implementation is to reduce the RAM usage while still having a full scale TCP. This making lwIP suitable for use in embedded systems with tens of kilobytes of free RAM and room for around 40 kilobytes of code ROM. lwIP was originally developed by Adam Dunkels at the Computer and Networks Architectures (CNA) lab at the Swedish Institute of Computer Science (SICS) and is now developed and maintained by a worldwide network of developers. FEATURES * IP (Internet Protocol, IPv4 and IPv6) including packet forwarding over multiple network interfaces * ICMP (Internet Control Message Protocol) for network maintenance and debugging * IGMP (Internet Group Management Protocol) for multicast traffic management * MLD (Multicast listener discovery for IPv6). Aims to be compliant with RFC 2710. No support for MLDv2 * ND (Neighbor discovery and stateless address autoconfiguration for IPv6). Aims to be compliant with RFC 4861 (Neighbor discovery) and RFC 4862 (Address autoconfiguration) * DHCP, AutoIP/APIPA (Zeroconf), ACD (Address Conflict Detection) and (stateless) DHCPv6 * UDP (User Datagram Protocol) including experimental UDP-lite extensions * TCP (Transmission Control Protocol) with congestion control, RTT estimation fast recovery/fast retransmit and sending SACKs * raw/native API for enhanced performance * Optional Berkeley-like socket API * TLS: optional layered TCP ("altcp") for nearly transparent TLS for any TCP-based protocol (ported to mbedTLS) (see changelog for more info) * PPPoS and PPPoE (Point-to-point protocol over Serial/Ethernet) * DNS (Domain name resolver incl. mDNS) * 6LoWPAN (via IEEE 802.15.4, BLE or ZEP) APPLICATIONS * HTTP server with SSI and CGI (HTTPS via altcp) * SNMPv2c agent with MIB compiler (Simple Network Management Protocol), v3 via altcp * SNTP (Simple network time protocol) * NetBIOS name service responder * MDNS (Multicast DNS) responder * iPerf server implementation * MQTT client (TLS support via altcp) LICENSE lwIP is freely available under a BSD license. DEVELOPMENT lwIP has grown into an excellent TCP/IP stack for embedded devices, and developers using the stack often submit bug fixes, improvements, and additions to the stack to further increase its usefulness. Development of lwIP is hosted on Savannah, a central point for software development, maintenance and distribution. Everyone can help improve lwIP by use of Savannah's interface, Git and the mailing list. A core team of developers will commit changes to the Git source tree. The lwIP TCP/IP stack is maintained in the 'lwip' Git module and contributions (such as platform ports) are in the 'contrib' Git module. See doc/savannah.txt for details on Git server access for users and developers. The current Git trees are web-browsable: http://git.savannah.gnu.org/cgit/lwip.git http://git.savannah.gnu.org/cgit/lwip/lwip-contrib.git Submit patches and bugs via the lwIP project page: http://savannah.nongnu.org/projects/lwip/ Continuous integration builds (GCC, clang): https://travis-ci.org/lwip-tcpip/lwip DOCUMENTATION Self documentation of the source code is regularly extracted from the current Git sources and is available from this web page: http://www.nongnu.org/lwip/ Also, there are mailing lists you can subscribe at http://savannah.nongnu.org/mail/?group=lwip plus searchable archives: http://lists.nongnu.org/archive/html/lwip-users/ http://lists.nongnu.org/archive/html/lwip-devel/ There is a wiki about lwIP at http://lwip.wikia.com/wiki/LwIP_Wiki You might get questions answered there, but unfortunately, it is not as well maintained as it should be. lwIP was originally written by Adam Dunkels: http://dunkels.com/adam/ Reading Adam's papers, the files in docs/, browsing the source code documentation and browsing the mailing list archives is a good way to become familiar with the design of lwIP. Adam Dunkels Leon Woestenberg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值