transport layer
Question: 为什么需要运输层? 运输层的作用? 它如何连接应用层和网络层?
transport layer为运行在不同主机上的应用进程之间提供了logic communication。运输层中的报文叫做segment.
因特网的transport layer中有两种协议:TCP和UDP。 UDP为调用它的应用程序提供了一种unreliable,connectionless的服务。 TCP则为调用它的应用程序提供了一种reliable,connection-oriented的服务。 之后会详细介绍这两种协议的异同。
multiplexing&demultiplexing
一些概念: demultiplexing:接收到segment的端系统将segment的数据交付到正确的socket的过程叫demultiplexing,是一个分发的过程。 multiplexing:在source host中从不同的socket中收集数据块,并为每个数据块封装上header生成segment传递到网络层的过程叫multiplexing。

在UDP中,一个socket是由一个two-tuple (destination IP address, destination port number) 来标识的。也就是说如果两个UDP segment有着相同的destination IP address和destination port number,那么这两个 segment将经过相同的destination socket被定向到相同的目的process。
在TCP中,一个socket是由一个four-tuple (destination IP address, destination port number, source IP address, source port number)标识的,
UDP
UDP除了复用/分解及少量的差错检测之外,基本上没有增加新的东西。
UDP被称为是connectionless的,目前我们所学到的协议是只有DNS是使用UDP的。但是UDP也是有它的优点的:
- Finer application-level control over what data is sent, and when.只要应用进程把数据传递给UDP,UDP就会把数据打包成segment并且传递给网络层,不像TCP有一个拥塞控制机制,时间上可能得不到保证。
- No connection establishment. UDP无需连接的建立,有建立连接的时延。
- Small packet header overhead. UDP的首部开销比TCP小,UDP的首部占8bytes,而TCP有20bytes.
UDP的segment结构是:

UDP Checksum
从上面的UDP segment可以得知,除了应用数据,segment有着4*2=8bytes的首部。这四个字段分别是:source port number, destination port number, length, Checksum。 其中长度就是指UDP segment的字节数。 那Checksum是如何计算的呢?
假设前三者的值分别是:
0110011001100000
0101010101010101
1000111100001100
先对前两个进行相加(相加如果有溢出要进行wrappend around回卷操作,所谓的wrapped around就是把溢出的数字加到最低位)。

再加上第三个数字:

我们得到了0100101011000010,对其求反得到checksum:1011010100111101。
在接收方如果要检验的话,就将这四个数字加在一起,如果得到的是1111111111111111,那么传输的过程中就没有出现差错。
TCP
在前两章已经介绍了三次握手的过程,这里就不提了,要注意的是TCP虽然是connection-oriented的,但是中间的网络元素并不会维持TCP连接状态,事实上,中间的路由器对TCP连接完全视而不见, TCP提供的是full-duplex service**,也就是说数据可以从A流向B也可以从B流向A,是双向的。 TCP的连接也总是point-to-point的,对于TCP而言,两个host是一对,三个或以上就太多了。
TCP的segment存在一个MSS(maximum segment size)限制,而MSS由MTU(maximum transmission unit)来设置。
TCP segment struture
TCP报文的结构为:

和UDP一样,TCP也有source port number, destination port number, length, Checksum。 但是TCP segment还包括下列字段:
- The 32-bit sequence number field and the 32-bit acknowledgment number field。这两个字段被用来实现可靠数据传输服务。
- The 16-bit receive window。 用于flow control
- The 4-bit header length field.
- The optional and variable-length options field
- The flag field contains 6 bits.
TCP报文段首都中最重要的字段是sequence number field和acknowledgment number field。
sequence number field是该segment首字节的字节流编号,而 acknowledgment number field就是接收方发给发送方下一步希望接收到的segment的sequence number field。
TCP拥塞控制
这个在之后的习题上再详细学习吧。
Exercise
R4
Q:Describe why an application developer might choose to run an application over UDP rather than TCP.
A:An application developer may not want its application to use TCP’s congestion control, which can throttle the application’s sending rate at times of congestion. Often, designers of IP telephony and IP videoconference applications choose to run their applications over UDP because they want to avoid TCP’s congestion control. Also, some applications do not need the reliable data transfer provided by TCP.
R5
Q:Why is it that voice and video traffic is often sent over TCP rather than UDP in today’s Internet? (Hint: The answer we are looking for has nothing to do with TCP’s congestion-control mechanism.)
A:Since most firewalls are configured to block UDP traffic, using TCP for video andvoice traffic lets the traffic though the firewalls.
R14
True or false?
c. Suppose Host A is sending Host B a large file over a TCP connection. The number of unacknowledged bytes that A sends cannot exceed the size of the receive buffer.
Yes
g. Suppose Host A sends one segment with sequence number 38 and 4 bytes of data over a TCP connection to Host B. In this same segment the acknowledgment number is necessarily 42.
False.
R15
R15. Suppose Host A sends two TCP segments back to back to Host B over a TCP connection. The first segment has sequence number 90; the second has sequence number 110. a. How much data is in the first segment? b. Suppose that the first segment is lost but the second segment arrives at B. In the acknowledgment that Host B sends to Host A, what will be the acknowledgment number?
a) 20 bytes b) ack number = 90