Differences Between TCP byte stream and UDP datagram, tcpudp
"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.
Why UDP is packet-oriented, while TCP is byte-oriented
UDP Packet
The UDP header consists of four fields, each of which occupies two bytes, as follows:
Source Port Number
Destination Port Number
Datagram Length
Check Value
The UDP protocol uses the port number to reserve its own data transmission channels for different applications. The UDP and TCP Protocols use this mechanism to support sending and receiving data simultaneously for multiple applications at the same time. The data sender (either a client or a server) sends UDP data through the source port, while the data receiver receives data through the target port. Some network applications can only use static ports reserved or registered for them in advance, while other network applications can use unregistered dynamic ports. Because the UDP header uses two bytes to store the port number, the valid range of the port number is from 0 to 65535. Generally, a port number larger than 49151 represents a dynamic port.
TCP packet
Each tcp contains the source port number and the target port number, plus the source ip address and the destination ip address in the ip header, and uniquely identifies a tcp connection. The serial number is used to identify the Data byte stream sent from the beginning of tcp to the tcp receiving end. It indicates the First Data byte in this packet segment. The serial Number field contains the Initial isn (Initial Sequence Number) of the connection selected by the host ). The first byte of data to be sent by the host. The serial number is isn + 1 because syn occupies a serial number.
IP package
The IPV4 header has 12 required fields and optional IP Option fields, before the data to be sent. If you use an existing library or other components in the IP layer, you do not need to consider most fields in the header, but the program code needs to provide the source and destination addresses.
1. Version (4 bits)
The IP protocol version has been revised several times. In 1981, RFC0791 described IPV4 and RCF2460 described IPV6.
2. header length (4 bits)
The header length is the length of the header data, expressed in 4 bytes, also in 32 bytes. The header length is variable. The required fields use 20 bytes (the header length is 5, and the IP option field can contain up to 40 additional bytes (the header length is 15 ).
3. service type (8 bits)
This field shows how the router is recommended to handle the report. You can select the maximum reliability, minimum latency, maximum throughput, and minimum overhead. The router can ignore this part.
4. datagram length (16 bits)
This field is the sum of Header Length and data bytes, in bytes. Maximum length: 65535 bytes.
5. identifier (16 bits)
A host originally assigned a unique datagram identifier for the datagram. When a data packet is sent to a destination address, if the router divides the data packet into a report, each report has the same data identifier.
6. Mark (3 bits)
Two of the Flag Fields are related to the tab.
Bit 0: unused.
Bit 1: Not a report. If this is 1, the router will not partition the data. The router tries its best to pass the datagram to the network that can receive the entire datagram at a time. Otherwise, the router will discard the datagram and return an error message, indicating that the destination address is inaccessible. The IP standard requires that the host can receive a datagram of less than 576 bytes. Therefore, if you want to pass the datagram to an unknown host and want to confirm that the datagram is not abandoned because of its original size, data of less than or equal to 576 bytes is used.
Bit 2: more messages. If this bit is 1, the datagram is a report, but it is not the last report of the part datagram; if this bit is 0, the datagram is not sharded, or the last report.
7. Patch offset (13 BITs)
This field identifies the location of the report in the part datagram. The value is 8 bytes. The maximum value is 8191 bytes, which corresponds to the offset of 65528 bytes.
For example, the 1024 bytes to be sent are divided into 576 and 424 bytes. The first offset is 0, and the second offset is 72 (because 72 × 8 = 576 ).
8. survival time (8 bits)
If a datagram does not reach its destination within a reasonable period of time, the network will discard it. The survival time field determines the time when the datagram is discarded.
The survival time indicates the datagram... the remaining full text>
What are the differences between IP packet tcp datagram udp datagram and their connection? Can I explain the details?
1,
An IP packet contains a tcp udp packet. An IP is a layer-3 (Network Layer) protocol. Both TCP and UDP belong to the layer-4 (Transport Layer) protocol.
TCP-the transmission control protocol provides connection-oriented and reliable byte stream services. 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.
UDP-the User Datagram Protocol 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. Key points:
A. Connection-based and connectionless
B. Requirements on system resources (more TCP and less UDP)
C. Simple UDP program structure
D. Stream mode and datagram Mode
E. TCP ensures data correctness, UDP may cause packet loss, TCP ensures data order, and UDP does not guarantee
3,
The packet sent by TCP has a serial number. After receiving the packet, the other party needs to send a feedback. If the packet has not received the feedback for a certain period of time, it will automatically execute timeout and resend the packet. Therefore, the biggest advantage of TCP is reliability. Generally, TCP is used for webpage (http), email (SMTP), remote connection (Telnet), and file (FTP) transmission.
UDP is a message-oriented protocol that does not need to be connected during communication. data transmission is naturally unreliable and is generally used for multi-point communication and real-time data services, such as voice broadcast, video, QQ, TFTP (simple file transfer), SNMP (Simple Network Management Protocol), RTP (Real-Time Transfer Protocol) RIP (routing information protocol, such as report to the stock market, aviation information), DNS (Domain Name explanation ). Focus on speed and smoothness.
To learn about TCP, you must know [three-way handshake, four-way bye-bye]. When you search for the Internet, the so-called three-way handshake means that the connection that must be established before sending data is called three-way handshake, it starts sending after the handshake is finished, which means connection-oriented.