[Computer network] Detailed TCP protocol

Detailed TCP protocol

Introduction:
The first part focuses on the blog udp protocol, let's review udp programming procedures and precautions
udp programming:
server:
1. Create a socket
2. The socket is bound to address information
3. Receive data request
4. Handling after the transmission data
5. Close the socket
client
1. Create a socket
2. the socket for the address bindings
3 sends a data request
4. When the server process, in response to the request
5. Close the socket
NOTICE matters]
1. udp programming, you must first start the server, because the characteristics of User Datagram Protocol is a connectionless unreliable, if you first start the client may cause data loss during transmission.
2. The client is not recommended to take the initiative to address binding, but when sending data to select the appropriate address information by the operating system to bind (the maximum extent possible to avoid port conflicts probability of)
the server must take the initiative to bind, because of the need to ensure that address never change the client can be found

tcp programming

Transmission characteristics: connection-oriented , reliable transmission, byte-oriented stream
this blog is mainly described in detail the connection-oriented content
here to use the connection-oriented classic and important and frequently asked three-way handshake, four wave process, go into detail later.

Transmission characteristics of TCP protocol (Detailed)

TCP transport layer protocol is a connection surface to provide high reliability byte streaming service, mainly used with a large number of packets to be exchanged transmission case.
In order to maintain the reliability of the transmission, TCP increased the number of overhead, such as: confirmation, flow control, timer and connection management.

End communications : TCP provides a connection-oriented interface application. End TCP connection when, at one client application, the server at the other end.
Establish a reliable connection : TCP client application requirements prior to exchange data with the server, connect the server to ensure reliable connection establishment, connection is established to test the network connectivity. If a fault occurs, hindering the packet arrives at the remote system, or the server does not accept connections, the connection attempt fails, the customer will be notified.
Reliable delivery: Once the connection is established, the order of delivery when the TCP guarantees data will be sent, not lost, there is no repetition, if not because of the failure to establish reliable delivery, the sender is notified.
Having a flow control transmission: TCP efficiency control data transmission rate of the transmission data reception rate to prevent fast with the recipient, and therefore can be used for TCP data from a computer to a fast computer slow transmission.
Duplex transmission : At any time, a single TCP connection allows simultaneous bidirectional transmission of data, but do not affect each other, the client may send a request to the server, and the server can be connected via the same transponder.
Flow mode : TCP receiver sends no message boundaries from the byte stream transmission directions.

TCP header
TCP data is encapsulated in an IP packet! ! ! As shown below:
Here Insert Picture Description
The following figure shows the data format of the TCP header, if the option field does not remember, they are usually 20 bytes.
Here Insert Picture Description
TCP header field meaning as (substantially control)
1. The source and destination port numbers : Each TCP segment contains a source port number and destination port number for the application process to find the transmitting end and receiving end.
2. The number and acknowledgment number: from the TCP sequence number indicates that the transmit byte stream data receiving side sends to the TCP, but indicates the first data byte in the message fields. If viewed as a stream of bytes between the two one-way flow of the application, the TCP byte stream for each sequence number counted. Number is a 32-bit unsigned number. The acknowledgment number is sent to confirm the end of the next sequence number expected to receive. Therefore, the acknowledgment number should be the last byte of data has been successfully received serial number plus one. Only the ACK flag is 1, the acknowledgment number field is valid.
3. The header length: header length header gives the number of 32-bit words. This option requires only because of the length of the field is variable. This field accounted for 4, so a maximum of TCP header of 60 bytes. If there is no option field, the normal length of 20 bytes.
4. Flag : There are six flags in the TCP header. More of them may be simultaneously set to 1, meaning are as follows:
Here Insert Picture Description
5. Window Size: TCP flow control is provided by each end of the connection through the window size of the statement. Window size for the specified number of bytes starting from the acknowledgment sequence number field value, this value is correct desired receiving the received byte number. Window size is a 16-bit field, and thus the maximum window size is 65535 bytes.
6. Inspection and: Inspection and covering the whole of the TCP segment containing TCP header, TCP pseudo-header and TCP data. This is a mandatory field, it must be calculated and stored by the sender, verification by the receiving end.
7. The urgent pointer: only when the flag is URG, urgent pointer is valid. Urgent pointer is a positive offset value is added to the sequence number field indicates the last byte of urgent data number. This is one way to transmit emergency data transmitting end to the other end.
8. Option: TCP Options section header is TCP to adapt to the complex network environment and better service application layer design options section up to 40 bytes. The most common option field is the maximum segment size (MaximumSegment, MSS). (That segment SYN flag is set to establish a connection) each connected party is usually reported in the first segment of communication specified in this option. It indicates the end of the present segment the maximum acceptable length.
9. Data: TCP data segment section is optional. For example, when the connection is established and the connection is terminated, the two sides exchanged only TCP segment header. Even if a party has no data to send, but also use the data without any header data to confirm receipt of. In many cases, the timeout process also transmitted without any data segment.

Client and server communication protocol tcp

Overall communication process

Server process:
call socket, create a file descriptor;
call bind, the current file descriptor and ip port (ip address and port) are bound together; if this port is already occupied by another process, it will bind failure;
call listen, this statement of the current file descriptor file descriptor as a server, do a good job preparing for the later accept;
call accecpt, and blocking , and other clients to connect over;
call send / recv send and receive data
calls close close the socket
client process:
call socket, create a file descriptor;
call [bind], empathy does not take the initiative to address bindings, assigned by the operating system
calls connect, connection is established successfully
send / recv send and receive data calls
calls socket close close

Communication flow

The client and server to establish a connection process, "three-way handshake" (emphasis compulsory)
| signal | effect |
URG urgent pointer is valid
ACK acknowledgment number is valid
PSH immediately prompts the receiver application reads data from the TCP buffer to go
RST other asked to re-establish the connection; we carry RST identifier called a reset packet
SYN requests to establish a connection; we carry SYN identifier called synchronization segment
FIN notice to the other, the end to be closed, we call identity is carrying FIN end segment

Detailed Version:
1. The client sends a SYN segment specified in the client wants to connect to the server port number, and the initial sequence number (the SequenceNumber the Initial, ISN), SYN requests a connection after transmission, the customer enters the SYN_SENT state waiting for the connection request.
2. First, after starting the server enters the LISTEN listening state, when it receives the SYN sent to the client request, enter SYN_RCV receives a request status , back SYN segment contains the initial sequence number of the server as a response, while recognizing the need to set the initial sequence number is incremented by one customer, the customer's confirmation SYN segment. A SYN will take a number.
3. After the client receives an acknowledgment packet enters the ESTABLISHED server (established) state, indicates that the party connection has been successfully established, the acknowledgment sequence number set to the client server ISN plus 1, the SYN segment to the server to confirm, when after the server receives the acknowledgment message, also entered eSTABLISHED (established) state
building process (Lite) connection:
call socket, create a file descriptor;
call connect, sends a connection request to the server;
connect will send a SYN segment and blocking wait server response; (first)
server receives a client's SYN, SYN-ACK will answer a segment that "agreed to establish connection"; (second)
will be () returned from the connect after the client receives the SYN-ACK, while a response ACK segment; (the third time)
the connection establishment procedure, commonly referred to as three-way handshake;
Here Insert Picture Description
transmission of process data
After the connection is established, TCP protocol provides full-duplex communications service; the so-called full-duplex mean, in the same connection, the same moment, communicating parties
can write data at the same time; relative concept called half-duplex, with a connection the same time, can only be written by one of the data;
the server from the accept () call to read immediately after return (), read socket is like reading the same pipe, if no data arrives on the block waiting;
then the client calls the write () sends a request to the server, the server receives () returns from read, for the client's request is processed, in this period
between the client calls the read () block waiting for reply from the server;
server calls write () will deal with the results back to the client, call read again () block and wait for the next request;
the client receives () returns from read, send a request to the next, so the cycle continues;
disconnection procedures (four wave) (emphasis compulsory) :
If the client no more requests, and calls the close () closes the connection, the client sends a FIN segment to the server (first);
after which the server receives FIN, a response to the ACK will, at the same time that a read will return 0 (a second times);
After the read returns, the server knows the client closed the connection, also calls the close connection is closed, this time the server sends a FIN to the client; (third time)
the client receives FIN, then returns an ACK to the server; (the first four)
this disconnection process, commonly referred to as "four wave"
Here Insert Picture Description
Why TIME WAIT state it?
1.TIME_WAIT extended the state of maintenance information current TCP connection for normal closing process correctly handle connected acknowledgment Wen loss is necessary.
2.TIME_WAIT state to ensure that the segments of the old connection will not be reused before disappearing on the network , so as to prevent disturbing the new connection.
Reference blog:
https://blog.csdn.net/L19002S/article/details/103667549

Published 35 original articles · won praise 107 · Views 8721

Guess you like

Origin blog.csdn.net/qq_43676757/article/details/105340617