文章目录

  • 前言
  • 一、TCP连接
    • 1.面向连接
    • 2.TCP报文段结构
    • 3.序号和确认序号
    • 4.Round-trip time and estimate time out
  • 二、可靠数据传输
  • 三、流量控制
  • 四、TCP连接管理
  • 总结

前言

As previously talked about reliable data transfer,We will by the reliable data transmission think ofTCP协议,本节就来讲述TCP协议的具体内容.


提示:以下是本篇文章正文内容,下面案例可供参考

一、TCP连接

1.面向连接

我们很清楚的知道TCP是面向连接的,Then why sayTCPIs a connection-oriented?原因就是TCPThe client and the server to communicate三次握手,Shake hands after they set up a communication connection,The transport of data can be used for both sides,On both sides of shaking hands must send each other prepare message segment of shaking hands,TCPThe connection is not in circuit-switched connection,It is not a physical connection,而是逻辑上的连接,It seems to us like a connection between them on a communications link,But in the router looks,This is just some packet. 而TCPConnection is how to set up,发起连接的进程称为客户进程,Receive the connection process is called the server process,The first notify the customer’s customer process transport layer,Want to be with the server in a process to establish a connection,To implement the code in the previous blog has been experimental code editor,Is through a socket interface to complete.

clientSocket.connect((serverName,serverPort))

Client and the server sends three messages,The first two message no payload,Is simply no application layer data,And the third message segment,Carrying the payload,This is called three-way handshake. After established a connection for the third time,Both sides can send message data each other,The client sends data through a socket interface flow,All the data through the door byTCPEstablished connection to transfer,TCPThe data lead toThe sender data cache,This sends the cache is established by a three-way handshake,接下来,TCPWill be sent from the cache every now and then randomly took out a piece of data transmission to the network layer,当TCP接收端收到数据后,Will add the data to theTCPConnect the receiver data cache.

2.TCP报文段结构

TCP报文段由首部字段和一个数据字段组成.数据字段包含一块应用数据.下图所示: 与UDP一致,TCPMessage segment contains first,The first contains the source port number and purpose port,It is used for multiplexing and demultiplexing.同时TCPAlso have inspection and field. 其中还包括:

  1. 32A bit of the serial number field、32A bit of confirmation number field(Behind here will focus on),These fields are used to achieve reliable data transmission service.
  2. 16Bits of receiving window field,该字段用于流量控制,用于指示接收方愿意接受的字节数量.
  3. 4比特的首部长度字段,This field only has32比特的字为单位的TCP首部长度.注意这里TCP首部长度是可变的.
  4. 可选与变长的选项字段,The field used to send defense receiver negotiation maximum message length(MSS)时,或在高速网络环境下用作窗口调节因子时使用.
  5. 6比特的标志字段.ACKBits are used to confirm only fields when effective,Also is successful to receive the message response.RST、SYN、FIN是用来连接、建立TCP与拆除TCP连接的.CWR与ECEBits are used in the explicit congestion notification.PSHWhen is setting,Indicating the receiver shall be immediately delivered to the upper.URGBits used to indicate a message paragraph there is sending the upper individuals to“紧急”的数据.紧急数据的最后一个字节由16比特的紧急数据指针字段指出.

If you see here may you in the first5A sign of something don’t understand,I see here is also have a lot of questions,但是没关系,We are down one by one to tell,We only know that there is the sign bit can.

3.序号和确认序号

In the above section when it comes toTCPMessage segment structure,Mention the serial number and confirmation number,Both the first field is theTCPTwo of the most important field in.也是TCPTo provide reliable data transmission key. 首先我们要知道,TCP把数据看成一个无结构的、有序的字节流,Serial number is based on the byte stream of transmission.A message for the serial number is the passage of the first byte stream of bytes number. Suppose you want to transfer a file500 000字节的,那么在mss(The biggest piece of data message)为1000的条件下,Data flow in the first byte number is0,The file is divided into the following: The first message segment number is0,And the second message is1000,以此类推. Know the concept of the serial number after,Now confirmed that the serial number,Confirm the serial number is more difficult than the serial number to understand a little,为了使得TCPA little more human,考虑到多种因素,Such as when you sendB的时候,May also be at the receivingBTo send you the data,And it is the confirmation number you expect fromBThe next byte received serial number. 举个简单的例子便于理解:If you have received0-567的字节数据,What do you expect the next data is567之后的数据,Is returned to youB的TCPThe first byte of the written confirmation number is568. In that case you may want to,如果B给我的是0-567,888-1024的字节的数据,那么中间的568-887的数据怎么办,You confirm the serial number what to write,Here we want to know a concept,TCPThe cumulative confirmed,如果你收到了0-567,888-1024的数据,Then you return data packets must be568The confirmation number.你可以理解为,You must request the byte stream is coherent serial number. One example is the more convenient understand: This example is simpleTelnet的TCP协议流程,在这里我们可以看到,用户键入‘C’,这里的序号是Seq=C的Ascii码,这个不是最重要的,We understand the most important thing is,A的确认序号ACK是79,B收到了A发送的42后,发现了AWant the next number is79,Returns the number ofA想要的79,而B确认了AReceive message isBWant to confirm the serial number is42后一位43,And then to go down,Confirm each other like this.

4.Round-trip time and estimate time out

既然说3.4Section in order toTCPLaid the foundation of the section,So how is laid the foundation,At this point we discuss a round-trip time and estimate time out,See here,请你想一个问题,If you use a timeout retransmission mechanism,The client how to estimate the waiting for the reply message time whether the overtime,Obviously the timeout interval must be greater than actually connect the round-trip time of,But in different scenarios and the actual situation,Every use of transmission round-trip time is not the same,So here are the actual cases, realize the timeout interval is greater than the connection of the round-trip time is a complex problem. 我们现在来看看,Actually message segment sampleRTT表示为SampleRTT激素hiBe issued from the newspaper article section(即交给IP)To confirm for the newspaper article section was the amount of time between the first knife,大部分的TCP的实现仅在某个时刻做一次SampleRTT的测量,Not every sent message section measuring aSampleRTT,In addition to note is emphatically,TCPAbsolutely not to have been the retransmission packet computationSAmpleRTT,在实际情况下,The router’s load changes,Or congestion degree change then swings.因此为了估计一个典型的RTTValue ofSampleRTT取平均值,这个称之为EstimatedRTT,公式如下: 新的EstimatedRTTValue is made by a certain weight beforeEstimatedRTTAnd at this stageSampleRTTThe value of the,在[RFC 6298]Presented in thisα推荐值是0.125.We can also see from the formula,这个对RTTThe average weighted to the last is about the recentSampleRTTThe influence of accounting for more than,This reason is unknown,Because the load of network congestion, of course, is according to the latest transfer rate change,The closer the sample to reflect the current network congestion situation. In addition to estimate thisRTT以外,测量RTT的变化也是有价值的,The following formulas definesRTT变化:

注意到DevRTT是一个SampleRTT与EstimatedRTTInterpolation betweenEWMA,如果SampleRTT波动较小,则DevRTTThe value of the small,Breeding a lot,这个β的值推荐为0.25. Here we have introduced theSampleRTT、EstimatedRTT、DevRTT的值,After we know the three values,Then the timeout retransmission timeout interval what?,Obviously the timeout interval must be greater thanEstimatedRTT,否则会造成不必要的重传,And a timeout interval should not be greater than too much,Otherwise, in a message after the period of lost,The retransmission packets sent too late,This will cause unnecessary delay,Therefore the time interval should be set toEstimatedRTTThe value of the combined with certain margin,当DevRTT波动较大时,This added allowance should be bigger,如果波动较小,Is the allowance of should be less. Recommended by the initial valueTimeoutInterval为1s,At the same time appear timeout after,This value is double,However, as long as received your message segment and updateEstimatedRTT,Use the formula to calculateTimeoutInterval的值.

二、可靠数据传输

IpService is to carry a message is not to provide reliable data transmission,Does not guarantee delivery of data,Don’t keep data sequential delivery,也不保证数据的完整性. TCPOn the basis of the established a reliable data transmission service,We through the following chart to show,并体验一下TCPProvided the reliable transport service: The first case as shown in the above,A发送序号为92字节数为8The data message segment,Confirm the serial number be is100,B接收到了A所发送的92号8Bytes of data message segment,并回复一个100序号的ACK确认报文,But the packet lost in transit,丢失后,A等待接收B发送的ACK超时,So resend this92号8字节的数据,这个时候B判断这个92No data has been actually receive,So rejected the sent92号字节数据,And returning to a100的ACK报文. The second case as shown in the above,AThe two packages were made byB正确的接收,BReturn to receive a messageACK,But back in the process of the twoACKPackage haven’t reached,对于92The time interval number is beyond the,When to start timing,只要BSend two packets to the next time interval is no retransmission second bag,If you see there will be a problem,Why don’t you preachACK100而传ACK120呢,我们往下看. A third type of situation such as the above,假设主机AAnd in the second case is exactly the same,发送两个报文段.The first message segment in the network is lost,But before the timeout received a second message segment,所以主机AKnow the second message segment120Bytes before all bytes have been received correctly,So the host won’t retransmission the two message.

三、流量控制

First flow control is why?之前我们提到过,一条TCP连接的每一侧主机都为该连接设置了接收缓存.当该TCP连接收到正确、按序的字节后,它就将数据放入接收缓存.Related data will be read from related application process,But you don’t have to be data bar as soon as I arrive to read,Likely related process is busy with other things.如果某应用程序读取数据时相对缓慢,The sender to send too much、太快,Send data will be very easy to make connection receive buffer overflow. 所以TCP提供了流量控制服务,Eliminate the possibility of sender buffer overflow.TCPLet the sender to maintain a variable called receiving window to provide flow control.通俗的说,接收窗口用于给发送方一个指示——该接收方还有多少可用的缓存空间.我们定义这个RcvBuffer来表示其大小.主机BThe application on the continuously read data from the cache.我们定义如下变量: LastByteRead:主机BOn the application of the process from the cache read the last byte code. LastByteRcvd:从网络中到达的并且已放入主机B接收缓存中的数据流的最后一个字节编号. 由于TCP不允许已分配的缓存溢出,下式必须成立: 接收窗口用rwnd来表示,根据缓存可用空间的数量来设置: Because the space is over time,所以rwnd是动态的.Below to variablerwndFor here: 首先主机B通过把当前的rwnd值放入它发给主机A的报文段中,Notify the hostAIn the connection of the cache and how many available space.开始时,主机B设定rwnd=RcvBuffer. 主机A轮流跟踪两个变量,LastByteSent和LastByteAcked,Notice the difference in value between the two is the hostA发送到连接中但未被确认的数据量.Through will not confirm the amount of data control at the receiving windowrwndThe value of,就可以保证主机A不会使主机B的接收缓存溢出. 那么现在还有一个问题,如果B接收的rwnd满了rwnd=0,则A停止发送,But the process is to read data from the cache,但AHow do you know now finally have the space,这里定义如果B的rwnd为0,那么AWill continue to send only1Bytes of data message segment,This message segment will be confirmed by the receiving party.这时候AWill be calculated inB还有多少rwnd值.

四、TCP连接管理

Here we will be more careful aboutTCP是如何建立-Demolition of aTCP连接的. 第一步:客户端的TCP向服务器端的TCPSent to a specialTCP报文段,It does not contain any application data,In a message section firstSYN标志位被置为1,这个报文段被称为SYN报文段. 第二步:一旦包含TCPSYN的报文段IPData to the server host,Server message segment fromTCP SYN报文段,For the distribution of the connection cache and variable,And return a allow link message segment,这个报文段也不包含应用层数据,这个报文段被称为SYNACK报文段. 第三步:在收到SYNACK报文段后,客户也要给该链接分配缓存和变量.客户主机则向服务器发送另外一个报文段;这最后一个报文段对服务器的允许连接的报文段进行了确认,The two willSYN比特置为0.

Of course in connection to both sides to end the connection,When the connection after the host of the“资源”(缓存中的变量)将被释放.Customer to issue a closing of the connection process instructions 则客户TCPSend a message to the server,Here the message section firstFIN被置为1.当服务器接收到该报文段后,就向发送方会送一个确认报文段.Then the server sends his own end message segment,其FINBit referred to as1.Finally the customer to confirm the server end message segment.At this time on the two hosts links are released. PS:Here I are wondering,Why after confirmation of the server returns again toFIN位置为1,Why can’t to1Helping to confirm back to thisACK报文呢,This is not a less communication. But the following presentation in fact I think also pretty normal,If is, indeed, to do so(按流程来,But I’m still refuses to…) 以上是客户端TCP经历的状态,Below is the server side state,Comparing the two together,We can understand.

总结

The code word code tired again,This chapter in fact: there are a lot of things to learn,TCPContains the knowledge is more,But keep looking at the picture,结合知识点,Understand more understand actually very easy to understand,书中最后还给出了SYN洪泛攻击,This is actually completed the first two shake hands do not do the third handshake,但这个不是重点,重点是了解TCP,But the end of the day actuallyTCPLearning isn’t completely end,And what to learn congestion after agreement,After that let’s say oh,加油.