01 -- UDP vs. TCP (part 1)

UDP vs. TCP

Introduction

Hi, I’m Glenn Fiedler and welcome to the first article in my article series Networking for Game Programmers

嗨,我是 Glenn Fiedler,欢迎来到“游戏网络编程”系列的开篇。


In this article we start with the most basic aspect of network programming, sending and receiving data over the network. This is just the beginning – the simplest and most basic part of what network programmers do, but still it is quite intricate and non-obvious as to what the best course of action is. Take care because if you get this part wrong it will have terrible effects on your multiplayer game!

在本文中,我们从最基本的网络编程概念开始,紧接着是通过网络发送和接收数据。这仅仅是千里之行的第一步,这是网络编程者日常中最简单最基础的部分了,但又是最佳操作过程中错综复杂的并非显而易见的部分。要当心哦!如果你在这一环节就搞错了,它将严重影响你的多人游戏编程!


You have most likely heard of sockets, and are probably aware that there are two main types: TCP and UDP. When writing a network game, we first need to choose what type of socket to use. Do we use TCP sockets, UDP sockets or a mixture of both?

你很可能听说过套接字,并且可能知道它们主要分两种类型:TCP 和 UDP。编写一款网游时,我们首先需要选择使用哪一种套接字。我们是使用 TCP 套接字,还是 UDP 套接字,或者是两者的混合体呢?


The choice you make depends entirely on what sort of game you want to network. So from this point on, and for the rest of this article series, I’m going to assume you want to network an action game. You know games like Halo, Battlefield 1942, Quake, Unreal, CounterStrke, Team Fortress and so on.

你的选择完全取决于你想要开发何种网络游戏。所以,基于此,本系列的其余文章中,我假定你想开发一款动作类网游。你了解像 Halo、Battlefield 1942、Quake、Unreal、CounterStrike、Team Fortress 之类的游戏。


In light of the fact that we want to network an action game, we’ll take a very close look at the properties of each type of socket, and dig a bit into how the internet actually works. Once we have all this information, the correct choice becomes very clear!

鉴于我们想开发一款动作类网游,我们将近距离观察每一种套接字的属性,然后稍微深挖 Internet 的工作原理。一旦我们拥有了这些信息,正确的选择就清晰可见了。


TCP/IP

TCP stands for “transmission control protocol” and IP stands for “internet protocol”. Together they form the backbone for almost everything you do online, from web browsing to IRC to email, its all built on top of TCP/IP.

TCP 代表“传输控制协议”,IP 代表“网络协议”。它们共同构成了你在网络上所作几乎所有事情的支柱,从网页浏览到即时聊天再到电子邮件,这些都建立在 TCP/IP 之上。


If you have ever used a TCP socket, then you’ll know that it is a reliable connection based protocol. This simply means that you make a connection between two machines, then you send data between the two computers much like you are writing to a file on one side, and reading from a file on the other.

如果你曾经使用过 TCP 套接字,那么你会知道它是一种可靠连接的协议。这就意味着你在两台机器之间建立连接,然后你在这两台机器之间发送数据,就如同你在向一端写文件而在另一端读文件一样。


This connection is reliable and ordered, meaning that all data you send is guaranteed to arrive at the other side in the same order that you wrote it. Its also a stream of data, meaning that TCP takes care of splitting up your data into packets and sending those across the network for you.

这种连接是可靠的有序的,这意味着你所发送的所有数据都能保证以书写时的同样顺序到达另一端,也就意味着 TCP 将你的数据拆分成数据包然后通过网络将它们发出去。


Again, remember its just like writing to a file. So simple!

重复一遍:请记住,它就像在写一个文件,如此简单!


IP

The simplicity is in stark contrast to what actually goes on at the lower level “IP” protocol underneath TCP.

与这一简单形态形成鲜明对比的,是 TCP 之下的 IP 协议所真正做的一切。


Here there is no concept of connection, instead packets are passed from one computer to the next. You can visualize this process like a hand-written note being passed from one person to the next across a crowded room, eventually reaching the person it is addressed to, but only after passing through many hands.

在这里(即 IP 协议层),没有连接的概念,取而代之的是从一台计算机往下一台传递数据包。你可以把这一过程虚拟成:在拥挤的房间里将手写的便条从一个人传递给下一个人,在经过多人之手后,最终抵达它所标注的那个人为止。


There is no guarantee that this note will actually reach the person it is addressed to. The sender just passes the note along and hopes for the best, never knowing whether or not the note was received, unless the other person decides to write back!

但是,无法保证这张便条能正确地抵达它所标注的那个人。发送者仅仅是传递了一张便条并满怀最好的希望而已,却永远不知道它是否被收到了,除非其他人决定写个回执便条。


Of course, it is in reality a little bit more complicated than this, since of course no one computer knows the exact sequence of computers to pass the packet along to so that it reaches its destination quickly. Sometimes “IP” passes along multiple copies of the same packet, these packets make their way to the destination via different paths, so will most likely arrive at different times.

当然,事实上,它比这要复杂一点点,因为没有哪个计算机知道快速通往目的地的一连串明确的计算机。有时,“IP“ 会传递多份相同的数据包,这些数据包通过不同的路径到达目的地,所以很可能在不同时刻抵达。


This is because the internet is designed to be self-organizing and self-repairing, able to route around connectivity problems. It’s actually quite cool if you think about what it is really going on at the low level. You can read all about this in the classic book TCP/IP Illustrated.

这是因为 Internet 被设计成自组织和自修复的,能够绕过有连接故障的节点。如果你想一想低层上所发生的,简直太炫酷了!你可以从阐述 TCP/IP 的经典书籍中读到这些的。


UDP

Instead of treating communications between computers like writing to files, what if we want to send and receive packets directly?

要是你想直接发送和接收数据包,而又不像写文件一样来处理计算机之间的联系,那么你会怎么做呢?


We can do this using UDP. UDP stands for “user datagram protocol” and it is another protocol built on top of IP, just like TCP, but this time instead of adding lots of features and complexity it is just a very thin layer over IP.

我们可以使用 UDP 来做。UDP 表示”用户数据报协议“,它是 IP 之上的另一个协议,就像 TCP 一样。但是,它没有添加大量的特性和复杂度,它仅仅是 IP 之上精简的一层。


With UDP we can send a packet to a destination IP address (eg. 112.140.20.10) and port (say 52423), and it will get passed from computer to computer until it arrives at the destination computer or is lost along the way.

我们可以使用 UDP 发送一个数据包到目的 IP 地址(例如 112.140.20.10)和端口(比如 52423),它将从一台计算机到另一台计算机直到它抵达目的主机或者半路丢失。


On the receiver side, we just sit there listening on a specific port (eg. 52423) and when a packet arrives from any computer (remember there are no connections!), we get notified of the address and port of the computer that sent the packet, the size of the packet, and can read the packet data.

在接收端,我们只是监听了特殊端口(比如 52423)坐等数据包从任意一台计算机传来(谨记这里是没有连接的哦!),当它到来时,我们才知道发送者的地址和端口、包的大小,然后才能读取包中的数据。


UDP is an unreliable protocol. In practice, most packets that are sent will get through, but you’ll usually have around 1-5% packet loss, and occasionally you’ll get periods where no packets get through at all (remember that there are lots of computers between you and your destination where things can go wrong…)

UDP 是一种不可靠的协议。实践中,我们发送的大多数数据包都将通过,但通常有大约 1-5% 的数据包丢失,而且偶尔会有一些时间段没有一个数据包通过(谨记你和目的地之间的众多计算机总会出点岔子)。


There is also no guarantee of ordering of packets. You could send 5 packets in order 1,2,3,4,5 and they could arrive completely out of order like 3,1,2,5,4. In practice, they will actually arrive in order almost all of the time, but again, you cannot rely on this!

它也无法保证数据包的顺序。你以 1,2,3,4,5 的顺序发送了 5 个数据包,它们可能完全以 3,1,2,5,4 的顺序到达。实践中,它们几乎会同时有序到达。但要重复一次:你不能依赖这些!


Finally, although UDP doesn’t do much on top of IP, it does make one guarantee for you. If you send a packet, it will either arrive in whole at the destination, or not arrive at all. So if you send a 256 byte packet to another computer, that computer cannot receive just the first 100 bytes of the packet, it must get the full 256 bytes of data. This is pretty much the only guarantee you get with UDP, everything else is up to you!

最后,尽管 UDP 没有在 IP 之上做太多,但它给了你一个保证:如果你发送了一个数据包,它要么整个到达目的地,要么整个都不到达。因此,若果你发送了一个 256 字节的数据包到另一台计算机,那台计算机不能仅仅接收数据包的前 100 字节,它必须接收到全部 256 字节。这是你用 UDP 能够得到的唯一保证了,其他的就看你啰!


转载于:https://my.oschina.net/deltatech/blog/535090

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值