TCP Literacy 1

 

TCP header format

 

 

 Source port number and destination port number is essential, and it is the same as UDP. Without these two port numbers. Data do not know which application should be sent. 

 

Why give the packet number it? Of course, in order to solve the problem of out of order. Not programmed to confirm how good number of which should come first, after which it should to it. Number is out of order to solve the problem.

 

It should also be some acknowledgment number is. The package should be sent out to confirm, or else how do I know that the other side has not received it? If you have not received it should be re-sent until served. This problem can be solved without packet loss.

 

For the TCP transport layer is tricky, but the problem of packet loss on the network layer of the pipe is less than, but will try to ensure reliable.

 

Then there are a number of status bits. Initiate a connection is, for example, SYN, ACK reply is, RST is reconnected, FIN is the end connections. TCP is a connection-oriented, so the two sides should maintain the status of the connection, sends these status bits with the package, it will cause a state change on both sides. 

 

Another important thing is the window size. Do TCP flow control, communications, each side declares a window, identify their current ability to deal with, and do not send too fast, Cheng Si me, do not send too slow, starve me.

 

In addition to doing other than flow control, TCP congestion control will do.

 

Order problems, steady not chaos;

Packet loss, commitment fly;

Connection maintenance, beginning and an end;

Flow control, sense of propriety;

Congestion control, known back into known.

 

TCP three-way handshake 

a: Hello, I am a

b: Hello, I am a b

a: Hello b

 

In most cases, A and B after the connection is established, A will send data immediately, once the A sends data, many problems have been resolved. A response sent to B, for example, lost, when sending data A subsequent arrival, B may think that this connection has been established, or are they hung up B, A sends data, it will being given that B is unreachable, then A B know a thing. 

 

You as a server B program designer for such a long time do not contract A client can take the initiative to shut down, thereby freeing up resources to use for other clients. 

 

Three-way handshake to establish issues of external connections, mainly in order to communicate one thing, that is, except the serial number of the TCP packet.

 

A tell B, No. package launched my face starting from the beginning of which the number, B also have to tell A, B number of packages starting from which initiated the beginning of the number. Why can not all numbers from 1 to start? Because it will always be a conflict. For example, even after the A B, it transmits the packet 1,2,3 three, but when the transmission 3, the intermediate lost, or detour, so resend, then A is down, even after re-B, No. 1 and from the beginning, and then send 2, but never thought to send 3, but the last detour that 3 back, sent to B, B natural that this is the next packet, then an error has occurred. 

 

Thus, each connection must have a different serial number. This number is the starting sequence number changes over time, can be seen as a 32-bit counter, every 4ms plus one, if the calculation, if the repeated need for more than 4 hours, the detour packet already dead Alice Alice, because we all know that the IP header which has a TTL, that is, survival time. 

 

 

 

At first, the client and server are in CLOSED state. First, the server actively listening on a port, in the LISTEN state. The client then initiates the connection SYN, SYN-SENT state after at. Server receives connections initiated return SYN, ACK and the client SYN, SYN-RCVD state in the following. After the client receives the SYN and ACK sent by the server, sending
the ACK ACK, then in ESTABLISHED state, because it pitch-catch success. After the server receives the ACK ACK, in the ESTABLISHED state, because it pitch-catch up.

 

 

TCP four wave 

A: B ah, I do not want to play. 

B: Oh, you do not want to play, ah, I know. 

A half-closed case

B: A ah, well, I do not play, bye. 

A: OK, bye.

Off, we can see that when A says, "do not play", they enter the state FIN_WAIT_1 after B receives "A do not play," the news, sending knew it, into the state of CLOSE_WAIT. 

 

A 收到“B 说知道了”,就进入 FIN_WAIT_2 的状态,如果这个时候 B 直接跑路,则 A 将永远在这个状 态。TCP 协议里面并没有对这个状态的处理,但是 Linux 有,可以调整 tcp_fin_timeout 这个参数,设 置一个超时时间。

 

如果 B 没有跑路,发送了“B 也不玩了”的请求到达 A 时,A 发送“知道 B 也不玩了”的 ACK 后,从 FIN_WAIT_2 状态结束,按说 A 可以跑路了,但是最后的这个 ACK 万一 B 收不到呢?则 B 会重新发一 个“B 不玩了”,这个时候 A 已经跑路了的话,B 就再也收不到 ACK 了,因而 TCP 协议要求 A 最后等 待一段时间 TIME_WAIT,这个时间要足够长,长到如果 B 没收到 ACK 的话,“B 说不玩了”会重发 的,A 会重新发一个 ACK 并且足够时间到达 B。 

 

A 直接跑路还有一个问题是,A 的端口就直接空出来了,但是 B 不知道,B 原来发过的很多包很可能还 在路上,如果 A 的端口被一个新的应用占用了,这个新的应用会收到上个连接中 B 发过来的包,虽然序 列号是重新生成的,但是这里要上一个双保险,防止产生混乱,因而也需要等足够长的时间,等到原来 B 发送的所有的包都死翘翘,再空出端口来。

 

等待的时间设为 2MSL,MSL是Maximum Segment Lifetime,报文最大生存时间,它是任何报文在网 络上存在的最长时间,超过这个时间报文将被丢弃。因为 TCP 报文基于是 IP 协议的,而 IP 头中有一个 TTL 域,是 IP 数据报可以经过的最大路由数,每经过一个处理他的路由器此值就减 1,当此值为 0 则数 据报将被丢弃,同时发送 ICMP 报文通知源主机。协议规定 MSL 为 2 分钟,实际应用中常用的是 30 秒,1 分钟和 2 分钟等。

 

还有一个异常情况就是,B 超过了 2MSL 的时间,依然没有收到它发的 FIN 的 ACK,怎么办呢?按照 TCP 的原理,B 当然还会重发 FIN,这个时候 A 再收到这个包之后,A 就表示,我已经在这里等了这么 长时间了,已经仁至义尽了,之后的我就都不认了,于是就直接发送 RST,B 就知道 A 早就跑了。

 

TCP状态机

 

 

 

end

Guess you like

Origin www.cnblogs.com/CherryTab/p/12198474.html
Recommended