Use difference
1. TCP is connection-oriented, while UDP is connectionless.
That is, there is no need to establish a connection before sending data
TCP provides reliable services. In other words, the data transmitted through the TCP connection is error-free, not lost, not repeated, and arrives in order. UDP makes every effort to deliver, but does not guarantee reliable delivery.
TCP is byte stream oriented. In fact, TCP treats data as a series of unstructured byte streams; UDP is message oriented.
UDP has no congestion control, so network congestion will not reduce the source host's sending rate (useful for real-time applications, such as IP telephony, real-time video conferencing, etc.)
Each TCP connection can only be point-to-point. UDP supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication.
The TCP header overhead is 20 bytes, and the UDP header overhead is small, only 8 bytes.
The logical communication channel of TCP is a full-duplex reliable channel, while UDP is an unreliable channel.
Different application scenarios
2. TCP is used when reliable transmission is necessary at the transport layer. UDP is mainly used for communications or broadcast communications that have high requirements for high-speed transmission and real-time performance.
Take an example of a call over IP telephony. If TCP is used, data will be resent if lost during transmission, which will result in the inability to smoothly transmit the caller's voice. With UDP, it will not perform retransmission processing, so there will be no problem of a large delay in the arrival of sound. Even if some data is lost, it will only affect a small part of the calls.
The above is the difference between TCP and UDP in php. I hope it will be helpful to everyone.