TCP and UDP transport layer protocol

img1

Author: HerryLo

Original Permanent link:  HTTPS: //github.com/AttemptWeb ...

TCP / IP protocol , you will often hear, which TCP (Transmission Control Protocol) is called Transmission Control Protocol, IP (Internet Protocol) protocol called the Internet .

img

In fact TCP / IP protocol, a protocol suite, is a set of protocols.

TCP / IP protocol that defines a set of rules. It defines how the entire Internet connection, how to communicate, how to negotiate the most basic rules.

TCP / IP protocols are divided into five layers, namely application layer, transport layer, network layer, data link layer, physical layer. Each have corresponding related protocols, wherein the two protocols are TCP and UDP transport layer.

Understanding of TCP and UDP

TCP/UDP
Since they are the transport layer protocol, of course, it is that their role is responsible for the transmission of data.

We imagine a scenario where both ends of the network are free to communicate, the transmission will take what way?

You can first imagine communication between humans.

The first is to play electric. I called the phone your phone until you answer "Hello", I then heard the voice, before continuing to talk between us.

Another is to send text messages. I sent a text message to your phone number, no way of knowing whether you receive, then I can only wait for your reply.

Wherein the first mode corresponds to TCP, and the second corresponding to UDP. TCP is required to confirm each other, that is, the need for three-way handshake (both ends of the transmission of the following three handshakes before the start of communication) prior to transmission. UDP with relatively rough, no matter what the other circumstances, sent directly, without confirmation process.

The image above to see, TCP is reliable, UDP is not reliable, what it means.

Reliable connection bring the decrease in efficiency. For example, a network request, a large part of them are actually wasting each other's confirmation process, consume more resources to ensure the reliable data transmission, and data transmission is ordered. Unreliable connections that bring improved efficiency, but may decline in quality of service.

要摆脱一种人为的误区,不要以为UDP不可靠,就没有应用场景。据说QQ发送数据就是靠UDP发送的,尽管是不可靠的连接,可是还是用了非常多的校验算法保证了数据质量稳定,同也保证了效率。TCP尽管耗时,可是对于稳定性优先的场景,还是应该有限选用TCP,比方浏览器中访问网页用的就是TCP。此外在一些长连接系统里面,比方微信,连接通道应该也是用TCP建立的,由于要维持一条稳定的信息传输通道。

协议是怎么控制,数据包是怎么传输,怎么校验数据的正确性,还有重传特性。这些都是协议中重要的控制过程,在这里不做详解,因为对于实际的产品并没有太大的用。至于如何选定传输数据方式,应该依据场景而定。

安全的东西往往不快,反之亦然。而TCP和UCD之间也是这样:

TCP(传输控制协议)

TCP(传输控制协议)是基于连接的。在正式收发数据之前,必须要与对方建立连接。这之间需要经过'三次握手':

第一步. 首先A主机需要和B主机之间建立数据收发,A需要发送一个请求询问B:'我要建立连接';

第二步. B收到消息后,会给A发送一个回应:'可以连接';

第三步. A收到B的回应后,A向B再次回应:'我要开始发送了',他们之间就可以建立连接了;
第一次握手的结论是:A的发送能力、B的接收能力正常。

第二次握手的结论是:B的发送能力、A的接收能力正常。

第三次握手的结论是:A的接收、发送能力,B的接收、发送能力都是正常的。

'3次握手'的作用就是 双方都能明确自己和对方的收、发能力是正常的.

UDP(用户数据报协议)

UDP (User Datagram Protocol) is a connectionless-oriented, not to establish a connection with each other , directly = datagram packet sent. UDP is suitable for transmission of a small amount of data, the reliability for the less demanding environments. Ping command to test the host used in daily life whether communication is normal, in fact 'ping' command of the principles is to send a UDP packet to the other host and peer to confirm the receipt of the packet, the packet is assumed that a message has arrived timely feedback back , the network transmission is normal.

Applicable scene

The TCP : full duplex, connection-oriented, reliable, and accurate control protocol. Mainly used for real-time performance is not strong, but the high transmission requirements of the application. For example: Web browsing, file downloading (not BT, computers download), sending e-mail and so on, these scenarios require TCP protocol for transmission. Of course, it overhead in the network is expensive.

UDP : This is not a reliable transport protocol. Because it does not sort the data segment to be transmitted, the data segment is not responsible for the order to reach the target (say it was not a reliable). It's a lot of overhead in the network, and therefore those UDP suitable for use in real-time, allowing the small mistakes occasions than TCP. For example: aspects of instant messaging (MSN, QQ), video, and other languages.

ps: the way to push your own personal public number: Yopai, we are interested can follow, from time to time updated weekly, can increase the pleasure of sharing the world

Guess you like

Origin www.cnblogs.com/liuheng/p/11291593.html