青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

分享知識

與大家一起分享知識

C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
  19 Posts :: 3 Stories :: 45 Comments :: 0 Trackbacks
TCP連接的建立和終止

為了幫助我們理解conncet,accept,close這幾個函數(shù),以及使用netstat工具來調試TCP應用程序,我們必須理解TCP連接是如何建立和終止的和TCP狀態(tài)轉換圖。

三次握手

當一個TCP連接建立時,發(fā)生了以下場景:

  1. The server must be prepared to accept an incoming connection. This is normally done by calling socket, bind, and listen and is called a passive open.

  2. The client issues an active open by calling connect. This causes the client TCP to send a "synchronize" (SYN) segment, which tells the server the client's initial sequence number for the data that the client will send on the connection. Normally, there is no data sent with the SYN; it just contains an IP header, a TCP header, and possible TCP options (which we will talk about shortly).

  3. The server must acknowledge (ACK) the client's SYN and the server must also send its own SYN containing the initial sequence number for the data that the server will send on the connection. The server sends its SYN and the ACK of the client's SYN in a single segment.

  4. The client must acknowledge the server's SYN.

最少需要三次包交換,因此稱作TCP的三次握手,如下圖所示:
圖示:?TCP 的三次握手

graphics/02fig02.gif

We show the client's initial sequence number as J and the server's initial sequence number as K. The acknowledgment number in an ACK is the next expected sequence number for the end sending the ACK. Since a SYN occupies one byte of the sequence number space, the acknowledgment number in the ACK of each SYN is the initial sequence number plus one. Similarly, the ACK of each FIN is the sequence number of the FIN plus one.

An everyday analogy for establishing a TCP connection is the telephone system [Nemeth 1997]. The socket function is the equivalent of having a telephone to use. bind is telling other people your telephone number so that they can call you. listen is turning on the ringer so that you will hear when an incoming call arrives. connect requires that we know the other person's phone number and dial it. accept is when the person being called answers the phone. Having the client's identity returned by accept (where the identify is the client's IP address and port number) is similar to having the caller ID feature show the caller's phone number. One difference, however, is that accept returns the client's identity only after the connection has been established, whereas the caller ID feature shows the caller's phone number before we choose whether to answer the phone or not. If the DNS is used (Chapter 11), it provides a service analogous to a telephone book. getaddrinfo is similar to looking up a person's phone number in the phone book. getnameinfo would be the equivalent of having a phone book sorted by telephone numbers that we could search, instead of a book sorted by name.

TCP 選項

Each SYN can contain TCP options. Commonly used options include the following:

  • MSS option. With this option, the TCP sending the SYN announces its maximum segment size, the maximum amount of data that it is willing to accept in each TCP segment, on this connection. The sending TCP uses the receiver's MSS value as the maximum size of a segment that it sends. We will see how to fetch and set this TCP option with the TCP_MAXSEG socket option (Section 7.9).

  • Window scale option. The maximum window that either TCP can advertise to the other TCP is 65,535, because the corresponding field in the TCP header occupies 16 bits. But, high-speed connections, common in today's Internet (45 Mbits/sec and faster, as described in RFC 1323 [Jacobson, Braden, and Borman 1992]), or long delay paths (satellite links) require a larger window to obtain the maximum throughput possible. This newer option specifies that the advertised window in the TCP header must be scaled (left-shifted) by 0–14 bits, providing a maximum window of almost one gigabyte (65,535 x 214). Both end-systems must support this option for the window scale to be used on a connection. We will see how to affect this option with the SO_RCVBUF socket option (Section 7.5).

    To provide interoperability with older implementations that do not support this option, the following rules apply. TCP can send the option with its SYN as part of an active open. But, it can scale its windows only if the other end also sends the option with its SYN. Similarly, the server's TCP can send this option only if it receives the option with the client's SYN. This logic assumes that implementations ignore options that they do not understand, which is required and common, but unfortunately, not guaranteed with all implementations.

  • Timestamp option. This option is needed for high-speed connections to prevent possible data corruption caused by old, delayed, or duplicated segments. Since it is a newer option, it is negotiated similarly to the window scale option. As network programmers there is nothing we need to worry about with this option.

These common options are supported by most implementations. The latter two are sometimes called the "RFC 1323 options," as that RFC [Jacobson, Braden, and Borman 1992] specifies the options. They are also called the "long fat pipe options," since a network with either a high bandwidth or a long delay is called a long fat pipe. Chapter 24 of TCPv1 contains more details on these options.

TCP 連接的終止

TCP建立時需要三次通知,而終止一個TCP連接時需要四次通知。
One application calls close first, and we say that this end performs the active close. This end's TCP sends a FIN segment, which means it is finished sending data.

  1. The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.

  2. Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.

  3. The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.

Since a FIN and an ACK are required in each direction, four segments are normally required. We use the qualifier "normally" because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment. We show these packets in Figure 2.3.

Figure 2.3. Packets exchanged when a TCP connection is closed.

graphics/02fig03.gif

A FIN occupies one byte of sequence number space just like a SYN. Therefore, the ACK of each FIN is the sequence number of the FIN plus one.

Between Steps 2 and 3 it is possible for data to flow from the end doing the passive close to the end doing the active close. This is called a half-close and we will talk about this in detail with the shutdown function in Section 6.6.

The sending of each FIN occurs when a socket is closed. We indicated that the application calls close for this to happen, but realize that when a Unix process terminates, either voluntarily (calling exit or having the main function return) or involuntarily (receiving a signal that terminates the process), all open descriptors are closed, which will also cause a FIN to be sent on any TCP connection that is still open.

Although we show the client in Figure 2.3 performing the active close, either end—the client or the server—can perform the active close. Often the client performs the active close, but with some protocols (notably HTTP/1.0), the server performs the active close.

TCP 狀態(tài)轉換圖

關于TCP連接的建立和終止操作可以由一個狀態(tài)轉換圖來詳細說明,如下圖所示:
圖示:TCP 狀態(tài)轉換圖
graphics/02fig04.gif

There are 11 different states defined for a connection and the rules of TCP dictate the transitions from one state to another, based on the current state and the segment received in that state. For example, if an application performs an active open in the CLOSED state, TCP sends a SYN and the new state is SYN_SENT. If TCP next receives a SYN with an ACK, it sends an ACK and the new state is ESTABLISHED. This final state is where most data transfer occurs.

The two arrows leading from the ESTABLISHED state deal with the termination of a connection. If an application calls close before receiving a FIN (an active close), the transition is to the FIN_WAIT_1 state. But if an application receives a FIN while in the ESTABLISHED state (a passive close), the transition is to the CLOSE_WAIT state.

We denote the normal client transitions with a darker solid line and the normal server transitions with a darker dashed line. We also note that there are two transitions that we have not talked about: a simultaneous open (when both ends send SYNs at about the same time and the SYNs cross in the network) and a simultaneous close (when both ends send FINs at the same time). Chapter 18 of TCPv1 contains examples and a discussion of both scenarios, which are possible but rare.

One reason for showing the state transition diagram is to show the 11 TCP states with their names. These states are displayed by netstat, which is a useful tool when debugging client/server applications. We will use netstat to monitor state changes in Chapter 5.

觀察包(Watching the Packets)

下土顯示了一個TCP連接實際發(fā)生的包交換情況:連接建立,數(shù)據(jù)傳輸和連接終止。在每一端傳輸?shù)臅r候,也給出了TCP狀態(tài)。?
TCP 連接的包交換

graphics/02fig05.gif

The client in this example announces an MSS of 536 (indicating that it implements only the minimum reassembly buffer size) and the server announces an MSS of 1,460 (typical for IPv4 on an Ethernet). It is okay for the MSS to be different in each direction (see Exercise 2.5).

Once a connection is established, the client forms a request and sends it to the server. We assume this request fits into a single TCP segment (i.e., less than 1,460 bytes given the server's announced MSS). The server processes the request and sends a reply, and we assume that the reply fits in a single segment (less than 536 in this example). We show both data segments as bolder arrows. Notice that the acknowledgment of the client's request is sent with the server's reply. This is called piggybacking and will normally happen when the time it takes the server to process the request and generate the reply is less than around 200 ms. If the server takes longer, say one second, we would see the acknowledgment followed later by the reply. (The dynamics of TCP data flow are covered in detail in Chapters 19 and 20 of TCPv1.)

We then show the four segments that terminate the connection. Notice that the end that performs the active close (the client in this scenario) enters the TIME_WAIT state. We will discuss this in the next section.

It is important to notice in Figure 2.5 that if the entire purpose of this connection was to send a one-segment request and receive a one-segment reply, there would be eight segments of overhead involved when using TCP. If UDP was used instead, only two packets would be exchanged: the request and the reply. But switching from TCP to UDP removes all the reliability that TCP provides to the application, pushing lots of these details from the transport layer (TCP) to the UDP application. Another important feature provided by TCP is congestion control, which must then be handled by the UDP application. Nevertheless, it is important to understand that many applications are built using UDP because the application exchanges small amounts of data and UDP avoids the overhead of TCP connection establishment and connection termination.

TIME_WAIT 狀態(tài)

毫無疑問,關于網(wǎng)絡編程中最讓人誤解點之一就是 TIME_WAIT?狀態(tài)。 在一端調用了close之后,該端維持這個狀態(tài)的時間為兩倍最大段生存時間(maximum segment lifetime (MSL))。

Every implementation of TCP must choose a value for the MSL. The recommended value in RFC 1122 [Braden 1989] is 2 minutes, although Berkeley-derived implementations have traditionally used a value of 30 seconds instead. This means the duration of the TIME_WAIT state is between 1 and 4 minutes. The MSL is the maximum amount of time that any given IP datagram can live in a network. We know this time is bounded because every datagram contains an 8-bit hop limit (the IPv4 TTL field in Figure A.1 and the IPv6 hop limit field in Figure A.2) with a maximum value of 255. Although this is a hop limit and not a true time limit, the assumption is made that a packet with the maximum hop limit of 255 cannot exist in a network for more than MSL seconds.

The way in which a packet gets "lost" in a network is usually the result of routing anomalies. A router crashes or a link between two routers goes down and it takes the routing protocols seconds or minutes to stabilize and find an alternate path. During that time period, routing loops can occur (router A sends packets to router B, and B sends them back to A) and packets can get caught in these loops. In the meantime, assuming the lost packet is a TCP segment, the sending TCP times out and retransmits the packet, and the retransmitted packet gets to the final destination by some alternate path. But sometime later (up to MSL seconds after the lost packet started on its journey), the routing loop is corrected and the packet that was lost in the loop is sent to the final destination. This original packet is called a lost duplicate or a wandering duplicate. TCP must handle these duplicates.

There are two reasons for the TIME_WAIT state:

  1. To implement TCP's full-duplex connection termination reliably

  2. To allow old duplicate segments to expire in the network

The first reason can be explained by looking at Figure 2.5 and assuming that the final ACK is lost. The server will resend its final FIN, so the client must maintain state information, allowing it to resend the final ACK. If it did not maintain this information, it would respond with an RST (a different type of TCP segment), which would be interpreted by the server as an error. If TCP is performing all the work necessary to terminate both directions of data flow cleanly for a connection (its full-duplex close), then it must correctly handle the loss of any of these four segments. This example also shows why the end that performs the active close is the end that remains in the TIME_WAIT state: because that end is the one that might have to retransmit the final ACK.

To understand the second reason for the TIME_WAIT state, assume we have a TCP connection between 12.106.32.254 port 1500 and 206.168.112.219 port 21. This connection is closed and then sometime later, we establish another connection between the same IP addresses and ports: 12.106.32.254 port 1500 and 206.168.112.219 port 21. This latter connection is called an incarnation of the previous connection since the IP addresses and ports are the same. TCP must prevent old duplicates from a connection from reappearing at some later time and being misinterpreted as belonging to a new incarnation of the same connection. To do this, TCP will not initiate a new incarnation of a connection that is currently in the TIME_WAIT state. Since the duration of the TIME_WAIT state is twice the MSL, this allows MSL seconds for a packet in one direction to be lost, and another MSL seconds for the reply to be lost. By enforcing this rule, we are guaranteed that when we successfully establish a TCP connection, all old duplicates from previous incarnations of the connection have expired in the network.

There is an exception to this rule. Berkeley-derived implementations will initiate a new incarnation of a connection that is currently in the TIME_WAIT state if the arriving SYN has a sequence number that is "greater than" the ending sequence number from the previous incarnation. Pages 958–959 of TCPv2 talk about this in more detail. This requires the server to perform the active close, since the TIME_WAIT state must exist on the end that receives the next SYN. This capability is used by the rsh command. RFC 1185 [Jacobson, Braden, and Zhang 1990] talks about some pitfalls in doing this.

一般網(wǎng)絡程序所使用的協(xié)議

下圖總結了一些:

Figure 2.19. Protocol usage of various common Internet applications.

graphics/02fig19.gif

The first two applications, ping and traceroute, are diagnostic applications that use ICMP. traceroute builds its own UDP packets to send and reads ICMP replies.

The three popular routing protocols demonstrate the variety of transport protocols used by routing protocols. OSPF uses IP directly, employing a raw socket, while RIP uses UDP and BGP uses TCP.

The next five are UDP-based applications, followed by seven TCP applications and four that use both UDP and TCP. The final five are IP telephony applications that use SCTP exclusively or optionally UDP, TCP, or SCTP.

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品v日韩精品| 91久久精品美女高潮| 欧美一区二区三区在线看| 国产精品久久久久久影院8一贰佰 国产精品久久久久久影视 | 国产在线精品一区二区中文| 久久精品夜夜夜夜久久| 欧美精品激情blacked18| 欧美一区二区三区在线观看| 欧美黄色网络| 免费观看一区| 欧美中文字幕在线观看| 欧美激情一区二区在线| 老司机免费视频一区二区三区| 午夜精品亚洲| 国产资源精品在线观看| 亚洲一区二区三区四区五区黄| 先锋影院在线亚洲| 亚洲高清成人| 国产精品一区二区久激情瑜伽| 中文日韩在线视频| 中文在线不卡视频| 国产精品拍天天在线| 久久青草欧美一区二区三区| 亚洲视频二区| 欧美三级网址| 欧美一级欧美一级在线播放| 久久久www| 在线视频精品一区| 欧美日韩美女| 久久免费精品视频| 国产精品久久久久久久久久三级| 国产精品入口麻豆原神| 欧美福利一区二区三区| 国产综合第一页| 久久精品免视看| 久久久久久久久综合| 欧美中文字幕在线视频| 久久久亚洲国产天美传媒修理工 | 国产精品久久久久久久久久三级 | 欧美成年网站| 国产精品尤物| 欧美国产日产韩国视频| 欧美激情精品久久久久久免费印度| 激情欧美日韩| 亚洲精品日韩在线观看| 欧美一区日本一区韩国一区| 亚洲精品专区| 亚洲人成毛片在线播放| 亚洲激情社区| 亚洲精品一区在线观看香蕉| 亚洲视频1区| 亚洲天堂av在线免费| 在线综合欧美| 久久成人免费网| 欧美激情欧美激情在线五月| 在线观看成人网| 亚洲视频你懂的| 亚洲午夜羞羞片| 亚洲国产免费看| 亚洲精品美女久久7777777| 国产在线欧美日韩| 日韩午夜高潮| 亚洲午夜一区二区三区| 午夜精品久久久久久久久久久久久| 国产婷婷色一区二区三区四区| 久久综合久久美利坚合众国| 一区二区日韩| 蜜臀a∨国产成人精品| 亚洲国产精品久久久久婷婷884| 女生裸体视频一区二区三区| 性久久久久久久久| 欧美a级片网| 国产精品午夜久久| 亚洲第一狼人社区| 伊人激情综合| 国产精品私房写真福利视频 | 母乳一区在线观看| 日韩午夜av电影| 亚洲精品日日夜夜| 国产精品久久| 欧美+亚洲+精品+三区| 亚洲婷婷在线| 亚洲欧美区自拍先锋| 亚洲国产三级| 久久色中文字幕| 欧美在线观看视频在线| 一本一本久久| 欧美性开放视频| 久久久久九九视频| 久久久久天天天天| 亚洲欧美国内爽妇网| 久久男人av资源网站| 亚洲精品欧美日韩| 一本一本a久久| 狠狠88综合久久久久综合网| 中文亚洲欧美| 亚洲黑丝一区二区| 亚洲精品一区二| 亚洲国产成人av好男人在线观看| 中文欧美字幕免费| 亚洲国产成人久久综合一区| 一区二区日韩伦理片| 亚洲毛片在线免费观看| 性欧美18~19sex高清播放| 亚洲视频中文字幕| 久久久欧美精品sm网站| 欧美一区深夜视频| 欧美性感一类影片在线播放| 亚洲国产黄色片| 1769国内精品视频在线播放| 免费日韩成人| 亚洲精品中文字幕有码专区| 亚洲欧美日韩国产一区二区三区| 欧美午夜精品久久久久久浪潮| 99re6这里只有精品| 久久精品观看| 亚洲男女自偷自拍图片另类| 亚洲影院免费| 99re6这里只有精品| 在线播放视频一区| 亚洲主播在线| 麻豆精品在线视频| 欧美午夜a级限制福利片| 亚洲一区影院| 国产精品久久91| 在线观看视频免费一区二区三区| 亚洲精品国产视频| 国内揄拍国内精品久久| 亚洲欧美日韩直播| 亚洲免费观看高清完整版在线观看熊 | 亚洲国产天堂久久国产91| 欧美一级专区| 欧美国产精品va在线观看| 亚洲综合日本| 欧美国产丝袜视频| 日韩亚洲国产欧美| 久久久综合视频| 亚洲女女女同性video| 久久精品国产第一区二区三区最新章节 | 在线国产精品一区| 免费在线观看成人av| 亚洲欧美日韩国产| 亚洲三级电影全部在线观看高清| 亚洲久久成人| 久久精品国产久精国产思思| 在线亚洲电影| 国产精品久久午夜| 亚洲一区二区三区午夜| 亚洲精品美女在线| 欧美成人嫩草网站| 亚洲欧美国产高清va在线播| 亚洲国产精品久久| 欧美电影电视剧在线观看| 久久资源av| 亚洲性图久久| 久久gogo国模裸体人体| 香蕉尹人综合在线观看| 国产精品电影在线观看| 一区二区三区欧美成人| 欧美日韩亚洲一区二区三区在线 | 亚洲电影在线看| 久久精品夜色噜噜亚洲aⅴ| 免费看成人av| 亚洲欧美不卡| 欧美日韩国产在线播放网站| 免费成人性网站| 欧美激情精品久久久久久久变态| 国产欧美日韩一区二区三区在线 | 久久激五月天综合精品| 91久久国产综合久久蜜月精品| 亚洲欧美日韩另类| 欧美精品日韩综合在线| 影音国产精品| 久久婷婷久久| 亚洲午夜久久久久久久久电影院| 美女精品在线观看| 亚洲综合电影一区二区三区| 久久综合狠狠| 欧美精品18+| 国产主播精品| 欧美在线观看视频一区二区| av成人黄色| 国产一区二区精品在线观看| 亚洲一区在线直播| 宅男在线国产精品| 国产偷自视频区视频一区二区| 亚洲最新在线| 日韩一级在线| 亚洲风情在线资源站| 国产精品盗摄一区二区三区| 国内精品99| 亚洲第一精品久久忘忧草社区| 久久看片网站| 亚洲伦理中文字幕| 亚洲视频电影图片偷拍一区| 国产精品裸体一区二区三区| 欧美xxx成人| 国产精品久久久久久福利一牛影视| 午夜精品美女久久久久av福利| 性做久久久久久久免费看|