tcp connection establishment and disconnection

       TCP protocol as one of the main transport layer protocol, with connection-oriented, end-to- end reliable full duplex communication protocol for the data transfer byte stream.

 

1, TCP segment

Although the interview TCP byte stream, TCP but data unit is transmitted segment. TCP segments into TCP header and data parts, the first 20 bytes of header TCP segment is fixed, followed 4n bytes are more and increase the options require a maximum of 40 bytes

 

  • Source and destination ports each accounted for two bytes, TCP is sub-functions realized through port.
  • No. 4 bytes, in the range [0,2 32 ], the TCP is a byte-oriented stream, each byte are numbered sequentially. For example, a segment, a sequence number field 201 carries the data length is 100, then the first data is a serial number 201, 300 is the last one. When the maximum range, and from 0 to start.
  • Acknowledgment number 4 bytes, it is desirable interface receives the next segment of the first byte of the sequence number. If the acknowledgment number = N, means that all the data before the number N has been received correctly.
  • Offset data four bits, representing the starting position of the data portion of the segment, the segment starting position from a distance. Indirectly indicates the length of the header.
  • Reserved accounted for 6, reserved for use, currently 0.
  • The URG (urgent) the URG =. 1, urgent pointer field indicates that valid data packet segment emergency, to be transmitted as soon as possible.
  • ACK ( acknowledgment) only when ACK = 1, an acknowledgment number is valid, the connection is established, all of the segments are an ACK.
  • PSH ( push) the recipient received PSH = segment 1, the application will be delivered as soon as possible to receive regular, without waiting for the entire cache fills again delivered. Less practical use.
  • RST ( reset) the RST = 1 indicates a serious error occurs in the TCP connection, if the connection must be re-link.
  • The SYN ( Synchronization) used to synchronize the serial connection is established. When SYN = 1, ACK = 0, indicates that the connection request is a segment. SYN = 1, ACK = 1 indicates consent of the other connections. TCP is used to establish a connection.
  • The FIN ( termination) for releasing a connector window. When FIN = 1, indicating that this segment sender does not send data requests the release of the one-way connection. TCP disconnected use.
  • Window 2 bytes indicates its own transmission receiving side window, the window value is used to tell each other allows the amount of data transmitted.
  • Checksum 2 bytes, field testing and examination include a header and a data portion.
  • Emergency correct occupies 2 bytes, URG = 1, Urgent Pointer indicates the number of bytes in the newspaper segment urgent data (number of bytes after the emergency general byte).
  • Option variable length up to 40 bytes. Example, the maximum segment size MSS. MSS refers to the length of the data portion of the TCP segment rather than the entire length, the default is 536 bytes long MSS. Expand the window, the time stamp options.

 

2, TCP connection is established - three-way handshake

 

First: The client sends a connection request packet to the server, where SYN = 1, seq = x. After YSN_END been sent into the state.

Second: After the server receives the message, it sends back an acknowledgment message, wherein the ACK = 1, ack = x + 1, because of the need to confirm a client, so the message has SYN =, seq = y 1 information. Send after entering SYN_RCVD state.

Third: the client receives the message, it sends an acknowledgment packet, where ACK = 1, ack = y + 1. After sending client enters ESTABLISHED state, the server receives the message, enter ESTABLISHED state. This, the connection is established.

 

3, TCP disconnect - Four waving

The first wave : the client sends a FIN = 1, seq = x packet to the server, and said he has no data to be transmitted, the transmission is connected to one side closed. Sent, the client enters FIN_WAIT_1 state.

Second wave : After receiving the service request packet, sends back ACK = 1, ack = x + 1 is the acknowledgment packet, to confirm disconnected. CLOSE_WAIT enter the server status. When the client receives the packet enters FIN_WAIT_2 state. At this point the data connection client to the server has been disconnected.

Third Wave : The service sends FIN = 1, seq = y of contracted clients, said he did not give the client the data. After entering LAST_ACK transmission state, waiting for an acknowledgment packet client.

The fourth wave : The client receives the request packet, transmits ACK = 1, ack = y + 1 of the packet acknowledgment server, and enters the TIME_WAIT state, it is possible to confirm packet to be retransmitted. After the server receives an acknowledgment packet, enter CLOSED state, end connection service client disconnected. The client will enter CLOSED state until after a period of time.

 

Guess you like

Origin www.cnblogs.com/ysuwangqiang/p/11485554.html