You get to know one article tcp and udp

Abstract: Fundamentals of Computer Networks

introduction

Each network protocol is a front-end engineer must master knowledge, TCP / IP, there are two transport layer protocols representative, namely TCP and UDP, this article will introduce this difference between the two as well as between them.

A, TCP / IP network model

Computers and network devices to communicate with each other, both sides must be based on the same method. For example, how to detect traffic to the destination from which side first initiates communication, the use of which language to communicate, how to end the regular communication needs to be determined in advance. Different hardware, communication between the operating system, all this will need a rule. And we put this rule is called protocol (protocol).

TCP / IP protocol suite is the general term for all types of Internet-related, such as: TCP, UDP, IP, FTP, HTTP, ICMP, SMTP protocols belong within the TCP / IP family.

TCP / IP model is the basis of the Internet, it is a general term for a range of network protocols. These protocols can be divided into four layers, respectively, a link layer, network layer, transport layer and application layer.

  • Link layer: responsible for packing and unpacking IP packets, sending and receiving ARP / RARP packets and the like.
  • Network Layer: is responsible for routing and destination network or host sends the packet to the packet.
  • Transport Layer: responsible for the packets are grouped and restructuring, and the TCP or UDP protocol to format packets encapsulated.
  • Application layer: is responsible for providing applications to users, such as HTTP, FTP, Telnet, DNS, SMTP and so on.

![](https://image.fundebug.com/2019-03-21-01.png)

In the network architecture to establish a network of communication must be conducted on both sides of the peer communication layer, not staggered. In the entire data transmission process, the data after the protocol header and trailer to be attached at the time of the respective layers on the layers at the sending end (only data link layer protocol need to be encapsulated tail) portion, that is, to the data encapsulation protocol, communication protocol is used to identify the corresponding layer. Next description TCP / IP transport protocols, there are two ---- the TCP and UDP representative.

Two, UDP

UDP stands for User Datagram Protocol protocol in the network as it is for processing and TCP protocol packet is a connectionless protocol. In the OSI model, in the fourth layer - transport layer protocol in the IP layer. There does not provide UDP packet grouping, assembly and disadvantages can not sort the data packet, that is, when the message is sent, it is not known whether the complete security arrived.

It has the following characteristics:

1. connectionless

First of all TCP and UDP do not need to be the same as three-way handshake to establish a connection, and want to send data can begin sending the data before sending. And only the data packets of the porter, the data packets will not split and any splicing operation.

Specifically, it is this:

  • At the transmitting end, the application layer passes data to the UDP transport layer protocol, UDP only increasing the data to a UDP header identifying the protocol is UDP, and then passed to the network layer
  • At the receiving end, the data is passed to the network layer to the transport layer, UDP header removing only IP packets will be passed to the application layer, without any splicing operation

2. unicast, multicast, and broadcast functions

UDP supports more than one mode of transmission, also supports many, many to many, many to one way, that UDP provides unicast, multicast, and broadcast functions.

3. UDP is a message-oriented

The sender of the UDP application to pay down the message, add a header in after delivery down the IP layer. UDP application layer packets down the post, neither combination nor split, but remains the packet boundaries. Therefore, the application must select the appropriate size of the packet

4. unreliability

First unreliability reflected in the connectionless communication does not need to establish a connection, like hair on the hair, such a situation is certainly not reliable.

What data is received and what data transfer, and it will not backup data, send the data will not care whether the other party has correctly received the data.

Also good times and bad network environment, but because there is no UDP congestion control, always will transmit data at a constant speed. Even if the network condition is not good, nor will the transmission rate adjustment. Such drawbacks realize is that in the case of poor network conditions may result in packet loss, but the advantages are obvious, high real-time requirements of certain scenes (such as conference calls) will need to use UDP instead of TCP.

![](https://image.fundebug.com/2019-03-21-02.gif)

Dynamic can be seen from the above chart, UDP will only want to send data packets will fall threw the other side, and do not care whether the safety and integrity of the data arrives.

The header overhead is small, the data packet transmission is efficient.

![](https://image.fundebug.com/2019-03-21-03.png)

UDP header contains the following data:

  • Two sixteen port number, source port, respectively (optional field) and destination port
  • The entire length of the message data
  • The entire data packet checksum (IPv4 optional field), the header field is used to find errors in the data and information

So UDP header overhead is small, only eight bytes twenty bytes at least much less compared to the TCP, when transmitting data packets is very efficient

Three, TCP

When a computer wants to communicate with another computer, communication between the two computers need clear and reliable, so as to ensure proper send and receive data. For example, when you want to view a Web page or check e-mail, and hope to complete in order to view the page without losing any content. When you download a file, hoping to get a complete file, not just part of a file, because if the data is missing or out of order, not the results you want, so it uses TCP.

TCP stands for Transmission Control Protocol protocol is a connection-oriented, reliable transport layer protocol based on a stream of bytes defined by the IETF RFC 793. TCP is connection-oriented, reliable stream protocol. Refers to the uninterrupted flow of data structures, you can think of it as drain water flow.

1. TCP connection process

As shown below, it can be seen that the process of establishing a TCP connection for the (three-way handshake procedure):

![](https://image.fundebug.com/2019-03-21-04.png)

The first handshake

The client sends a connection request to the server segment. In packet data communications segment comprising its initial sequence number. After sending the request, the client will enter the SYN-SENT state.

The second handshake

After the server receives a connection request message segment, if the agreed connection, an acknowledgment is sent, the reply also contains its own data in the initial communication sequence number after transmission completion state into the SYN-RECEIVED.

Third handshake

When answering the client receives a connection agreement, but also to send to the server a confirmation message. Client End made this segment after entering ESTABLISHED state, the server receives this response also after entering ESTABLISHED state, then the connection is established.

Here we may have more doubts: Why establish a TCP connection requires three-way handshake, instead of twice? This is because it is connected to prevent the case where a failure occurs in the request message received by the server segment is to generate an error.

![](https://image.fundebug.com/2019-03-21-05.gif)

2. TCP broken link

![](https://image.fundebug.com/2019-03-21-06.png)

TCP is a full-duplex, and both ends need to send FIN ACK while disconnected.

The first handshake

If Client A that the data transmission is completed, it needs to send a connection release request to the server B.

The second handshake

After B receives the connection release request, it will tell the application layer to release the TCP connection. Then sends an ACK packet, and enters CLOSE_WAIT state, at this time indicates that the connection A to B has been released, no longer receives the A data sent. But because the TCP connection is bidirectional, so B can still send data to A.

Third handshake

If B is not finished at this time there will continue to send data, it sends a connection release request, then B will enter the state to the LAST-ACK After completion of the A.

Fourth handshake

A release request is received after sending an acknowledgment to the transponder B, this time into the A TIME-WAIT state. The state will continue 2MSL (maximum segment lifetime, time refers to the segment live in the network, the timeout will be abandoned) time, if the time period does not request retransmission of B, then it enters the CLOSED state. When B receives the acknowledgment, it will enter the CLOSED state.

Features 3. TCP protocol

  • Connection-oriented

    Connection-oriented, means must be established before sending the data across the connection. Establishing a connection is the "three-way handshake", this will establish a reliable connection. Connection is established, it is to lay the foundation for the reliable transmission of data.

  • Only supports unicast transmission

Each TCP transport connection only has two ends, can only point to point data transmission, does not support multicast and broadcast transmission.

  • Byte stream and

Unlike TCP UDP packet as one transmitted independently the same, but to be transmitted in a byte stream without reservation packet boundaries.

  • Reliable transport

    For reliable transmission loss is determined, the error by the TCP acknowledgment number and a block number. To ensure reliable TCP packet transmission, give each packet a sequence number, but also ensures that the serial number sent to the receiver sequentially receiving packet entity. Then the receiving side has successfully received byte entity sends back a corresponding acknowledgment (the ACK); If the sender entity does not receive the acknowledgment within a reasonable round-trip time (RTT), then the corresponding data (assuming lost) It will be retransmitted.

  • Provide congestion control

When network congestion occurs, TCP can be reduced injection rate and amount of data to the network, congestion relief

  • TCP provides full-duplex communication

TCP allows communication application can send data between the two sides at any time, because both ends of the TCP connection has a buffer for temporary storage of data two-way communication. Of course, TCP may immediately transmit a data segment, the cache may be a period of time to send more data segments (depending on the maximum segment size MSS)

Four, TCP and UDP compare

1. Comparison

UDP TCP
Is connected not connected Connection-oriented
Is reliable Unreliable transport, no flow control and congestion control Reliable transmission, flow control and congestion control
The number of connection objects Support one to one, one to many, and many-to-many interactive communication Only one communication
transfer method Message-oriented Byte stream and
Header overhead Header overhead small, only 8 bytes Minimum 20-byte header, 60 bytes of the maximum
Applicable scene Suitable for real-time applications (IP telephony, video conferencing, broadcast, etc.) Applications that require reliable transmission applications, such as file transfer

2. summary

  • TCP provides reliable connection-oriented service to the upper layer, UDP provides connectionless unreliable service to the upper layer.
  • Although the UDP and TCP transport does not come accurately, but also can make a difference in a lot of places with high real-time requirements
  • High requirements for data accuracy, speed may be relatively slow, you can use TCP

Reference articles and books

  • [Computer network simple tutorial and simulation] (https://www.bilibili.com/video/av40163489?from=search&seid=11224550652008575322)
  • [TCP and UDP difference] (https://www.bilibili.com/video/av31151326?from=search&seid=11224550652008575322)
  • [Interview distal channel] (https://juejin.im/book/5bdc715fe51d454e755f75ef/section/5bdc729af265da615a414603)
  • [Article understand the relationship between TCP / IP, TCP, UDP, IP, Socket] (https://blog.csdn.net/freekiteyu/article/details/72236734)
  • Depth understanding of computer network
  • Characteristics and differences TCP and UDP protocols Detailed

Guess you like

Origin www.cnblogs.com/wwbplus/p/11794854.html