TCP/UDP如何处理丢失的比特?

huangapple go评论59阅读模式
英文:

How does TCP/UDP deal with lost bits?

问题

根据我找到的信息,TCP/UDP数据包没有任何分隔符来指示数据包的起始/结束。那么,如果在传输过程中丢失了一个字节,会发生什么情况?这不会破坏后续的所有数据包吗?

虽然TCP/UDP有组织良好的数据包结构,但数据包仍然需要序列化为二进制,每个位逐位通过以太网电缆/无线连接发送。假设我们有一个长度字段为50字节的数据包。在传输过程中,有效载荷部分丢失了8位(即1字节)。接收方收到数据包头部,看到需要接收50字节。它只接收到了49字节,并等待最后一个字节才考虑数据包已接收。与此同时,第二个数据包的二进制数据被发送到电缆上。接收方仍在等待第一个数据包的最后一个字节,它会将第二个数据包的第一个字节误认为是第一个数据包的最后一个字节。现在第一个和第二个数据包都被破坏了,很可能接收到的大多数,如果不是全部,附加的数据包都会因为发送方和接收方相差一个字节而被破坏。

我无法想象这实际上是一个现实世界的问题,所以显然我在理解TCP/UDP在网络传输中的工作方式方面存在某种误解。

英文:

From what I can find, TCP/UDP packets do not have any delimiters to indicate the start/end of a packet. So what happens if, during transmission, a byte is lost? Wouldn't this corrupt basically all following packets?

While TCP/UDP has an organized packet structure, the packet still needs to be serialized into binary, and each bit is sent one by one over the ethernet cable / wifi connection. Let's say we have a packet where the length field is 50 bytes. During transmission, 8 bits (aka 1 byte) in the payload section are flat out lost (not flipped). The receiver gets the packet header, sees that it needs to receive 50 bytes. It only receives 49 bytes, and gets stuck waiting for the last byte before considering the packet to be received. Meanwhile, a second packet's binary data is sent over the wire. The receiver, still waiting for the last byte from the first packet, steals the first byte from the second packet thinking its actually the last byte from the first packet. Now both the first and second packet are corrupted, and it is highly likely that most, if not all, additional packets that are received are corrupted because the sender and receiver are out of sync by one byte.

I can't imagine this is actually a real world problem, so obviously I'm missing something in my understanding of how TCP/UDP works over the wire.

答案1

得分: 3

TCP和UDP数据包不会直接写入线路;相反,每个数据包都会被传递给一个更低级的网络系统(比如以太网或蓝牙)。然后,这个更低级的系统要么负责传递整个数据包,要么检测到数据包损坏并要么修复它,要么丢弃损坏的数据包。您可以搜索“以太网数据包封装”以了解以太网如何实现这种逻辑。

英文:

TCP and udp packets aren’t written to the wire directly; rather each packet is handed off to a lower-level networking system (such as Ethernet or Bluetooth). The lower-level system is then responsible for either delivering the whole packet, or detecting corruption and either fixing it or dropping the corrupted packet. You might Google “Ethernet packet framing” for an example of how Ethernet implements that logic.

答案2

得分: 0

网络工作的方式意味着要么接收整个数据包,要么不接收。UDP 不可靠,意味着如果数据包没有接收到,你不知道它没有接收到。TCP 更可靠,它确保所有发送的数据包都被接收,并且按正确顺序接收。当你在代码中接收 UDP 数据包时,如果你只将数据包接收到一个 1000 字节的缓冲区,但数据包长达 2000 字节,多余的字节会丢失。TCP 是一个流,所以多余的部分会保存到下一次读取调用中。

似乎你的问题更与 IP 数据包如何保持完整有关。这是通过路由器/服务器在通信路径上如何交流来实现的。如果它们超负荷,它们有时会跳过一个数据包,但一旦开始监听一个数据包,它们就会接收整个数据包。

英文:

The way that networking works means that either an entire packet is received or nothing is received. UDP is unreliable meaning that if that packet isn’t received, you don’t know it isn’t received. Tcp is more reliable in that it will make sure that all packets that are sent are received, and in the right order. When you are receiving a UDP packet in code, if you only receive a packet into a 1000 byte buffer, but the packet is 2000 bytes long, the excess bytes are lost. TCP is a stream, so the extra is kept for the next read call.

It seems like your question is more related to how IP packets are kept intact. This is done through how the different routers/servers on a route communicate with each other. They will sometimes skip a packet if they are overloaded, but once they start listening for a packet, they will receive the entire thing.

huangapple
  • 本文由 发表于 2023年4月13日 22:52:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定