TCP 和UDP 的区别

偶然发现我们用的是UDP, 原因是TCP延时太大, 补一把基础知识。。

Difference between TCP and UDP

TCP UDP
Reliability: TCP is connection-oriented protocol. When a file or message send it will get delivered unless connections fails. If connection lost, the server will request the lost part. There is no corruption while transferring a message. Reliability: UDP is connectionless protocol. When you a send a data or message, you don’t know if it’ll get there, it could get lost on the way. There may be corruption while transferring a message.
Ordered: If you send two messages along a connection, one after the other, you know the first message will get there first. You don’t have to worry about data arriving in the wrong order. Ordered: If you send two messages out, you don’t know what order they’ll arrive in i.e. no ordered
Heavyweight: – when the low level parts of the TCP “stream” arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together. Lightweight: No ordering of messages, no tracking connections, etc. It’s just fire and forget! This means it’s a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets.
Streaming: Data is read as a “stream,” with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call. Datagrams: Packets are sent individually and are guaranteed to be whole if they arrive. One packet per one read call.
Examples: World Wide Web (Apache TCP port 80), e-mail (SMTP TCP port 25 Postfix MTA), File Transfer Protocol (FTP port 21) and Secure Shell (OpenSSH port 22) etc. Examples: Domain Name System (DNS UDP port 53), streaming media applications such as IPTV or movies, Voice over IP (VoIP), Trivial File Transfer Protocol (TFTP) and online multiplayer games etc
### TCP协议与UDP协议的主要区别 TCP(Transmission Control Protocol,传输控制协议)UDP(User Datagram Protocol,用户数据报协议)是两种常用的传输层协议,它们各自具有不同的特性、功能以及适用场景。 #### 1. 连接模式 TCP是一种面向连接的协议,在正式收发数据前必须先建立可靠的连接,这一过程通过三次握手完成[^1]。而UDP则属于无连接的协议,无需事先建立连接即可直接发送数据包[^3]。 #### 2. 可靠性 TCP提供了高可靠性的数据传输服务,它能够确保数据按序到达目标设备,并且具备重传丢失数据的功能[^4]。相比之下,UDP并不保证数据包的顺序性完整性,可能会发生丢包或乱序现象[^2]。 #### 3. 性能表现 由于TCP需要维护连接状态并执行复杂的错误检测机制,其性能相对较低;然而这些额外操作正是为了保障数据传输的质量所必需的。相反地,UDP因为省去了许多控制环节,所以在速度方面占据优势,尤其适合那些可以容忍一定范围内的数据损失的应用场合[^3]。 #### 4. 首部开销 从头部结构来看,TCP头部较为复杂,包含了源端口号、目的端口号等多个字段用于实现各种高级功能;而UDP头部比较简单轻量级,仅需四个固定长度字段就能满足基本需求。 ### 适用场景分析 - **Web浏览**: 当用户利用浏览器访问互联网站点时,HTTP/HTTPS这类高层应用依赖于底层稳定的TCP通道来传递请求响应消息流,从而确保存取页面内容时不被中断或者损坏[^2]。 - **邮件系统**: SMTP(Simple Mail Transfer Protocol), IMAP(Internet Message Access Protocol) POP(Post Office Protocol) 等电子邮件相关标准均采用TCP作为基础载体,以保护信件资料免受中途遗失风险影响[^2]。 - **视频会议&在线游戏**: 对于此种强调即时互动效果的服务形式而言,即使偶尔存在轻微的画面跳帧亦可接受,因此选用快速高效的UDP更为合适。 - **DNS查询**: 域名解析服务经常运用短小精悍的UDP分组来进行高效快捷的操作处理[^3]。 ```python import socket def create_tcp_socket(): tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) return tcp_sock def create_udp_socket(): udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return udp_sock ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值