Differences between TCP byte stream and UDP datagram

Source: Internet
Author: User
"TCP is a stream-mode protocol and UDP is a datagram-mode protocol". I believe you are familiar with this sentence ~ However, what is the difference between "stream mode" and "data packet mode" in programming? The following is my understanding for your reference only! 1. TCP stands for a metaphor for TCP. you have a reservoir in your house. you can pour water in it. there is a faucet in the reservoir. you can put the water in the pool through the faucet, and then use a variety of containers (

"TCP is a stream-mode protocol and UDP is a datagram-mode protocol". I believe you are familiar with this sentence ~ However, what is the difference between "stream mode" and "data packet mode" in programming? The following is my understanding for your reference only!

1. TCP

For example, TCP. you have a reservoir in your house. you can pour water in it. there is a faucet in the reservoir. you can put the water in the pool through the faucet, then use a variety of containers (cups, mineral water bottles, pots and pans) to connect to the water.

In the above example, pouring water several times into the pool is not necessarily related to taking the water several times. that is to say, you can only pour the water once and then pick it up 10 times. In addition, the amount of water in the pool will be less; the amount of water poured into the pool will increase, but it cannot exceed the pool capacity, the excess water will overflow.

In combination with the concept of TCP, the pool is like receiving a cache. pouring water is equivalent to sending data, and receiving water is equivalent to reading data. For example, if you send data to the other end through a TCP connection, you only call write once and send 100 bytes, but the other end can receive 10 bytes each time; you can also call 10 write times, with 10 bytes each time, but the other party can finish it at one time. (Assuming all data can be reached) However, the data volume you send cannot be larger than the recipient's receiving cache (traffic control). If you want to send excessive data, when the other party's cache is full, the extra data will be discarded.

2. UDP

Different from TCP, UDP calls write several times, and the receiving end must read the data with the same number of reads. UPD is based on packets. when receiving a message, only one packet can be read at most at a time. messages and packets are not merged. if the buffer zone is smaller than the packet length, the extra part will be discarded. That is to say, if the MSG_PEEK flag is not specified, each read operation consumes one packet.

3. Why?

In fact, this difference is determined by the characteristics of TCP and UDP. TCP is connection-oriented. that is to say, during the connection continuity process, the data received by the socket is sent by the same host (hijacking is not considered). therefore, you only need to know how much data is read each time.

UDP is a connectionless protocol, that is, any host can send data to the acceptor as long as it knows the IP address and port of the acceptor and the network is reachable. At this time, if the data of more than one packet can be read at a time, it will be messy. For example, if host A sends packets P1 and host B sends packets P2, then, the data of P1 and P2 will be merged. this data is meaningless.

Other differences between the two protocols

TCP (Transmission Control Protocol) Transmission Control Protocol:

This protocol is mainly used to establish a virtual connection between hosts to achieve highly reliable packet exchange. The IP protocol can split and assemble IP data packets, but the IP protocol cannot clearly determine whether the packets are successfully sent to the target computer. The TCP protocol is different. in the protocol transmission mode, after the packet is successfully sent to the target computer, TCP requires a confirmation. If no confirmation is received within a certain time limit, TCP resends the data packet. In addition, TCP can recover unordered, lost, and damaged data packets during transmission.

The transport control protocol is a connection-oriented, reliable, and byte stream-based transport layer communication protocol, which is generally described by RFC793 of IETF. In the simplified computer network OSI model, it completes the functions specified by the transport layer.

User Data Protocol (UDP:

User Datagram Protocol (UDP) is a connectionless transport layer protocol in the ISO Reference Model. It provides a simple and unreliable information transmission service for transactions. UDP is basically an interface between the IP protocol and the upper-layer protocol. The applicable port of UDP protocol identifies multiple applications running on the same device.

Since most network applications run on the same machine, the computer must be able to ensure that software programs on the machine at the destination can obtain data packets from the machine at the source address, and the source computer can receive the correct response. This is done by using the UDP port number.

Differences:

1. connection-based and connectionless

TCP-the transmission control protocol provides a connection-oriented and reliable byte stream service. Before the customer and the server exchange data with each other, a TCP connection must be established between the two parties before data can be transmitted. TCP provides timeout resend, discard duplicate data, test data, traffic control, and other functions to ensure data can be transferred from one end to the other.

The transmission process of each data packet is: first establish a link, data transmission, and then clear the link. The data packet does not contain the destination address. The receiving and sending ends are not only in the same order, but also in the same content. It has high reliability.

UDP-the User Datagram Protocol is connectionless. each packet has a complete source, destination address, and group number. each packet is transmitted independently in the network regardless of its order, after the data arrives at the receiving end, it is sorted and assembled. in case of loss, error, or out-of-order data, it is resolved by sending a request again. It is efficient.

It is a simple datagram-oriented transport layer protocol. UDP does not provide reliability. it only sends the data from the application to the IP layer, but it cannot guarantee that the data can reach the destination. Because UDP does not need to establish a connection between the client and the server before transmitting the Datagram, and there is no timeout and re-transmission mechanism, the transmission speed is very fast.

2. requirements on system resources (more TCP and less UDP)

3. simple UDP program structure

4. stream mode and Datagram mode

5. TCP ensures data correctness, UDP may cause packet loss, TCP ensures data order, and UDP does not guarantee

6. TCP is a reliable byte stream service. UDP does not provide a reliable mechanism for IP protocol, traffic control, and error recovery.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.