Implementation of TCP and UDP transmission instances in Android
TCP and UDP are very important in network transmission, and they are equally important in Android development.
First, let's take a look at what TCP and UDP are.
What is TCP?
TCP: transmission control protocol TCP is a connection oriented, reliable and byte stream based transport layer communication protocol, which is specified by IETF RFC 793. In the simplified computer network OSI model, it completes the functions specified by the fourth transport layer. The application layer sends the data stream represented by 8-bit bytes for inter network transmission to the TCP layer, and then TCP divides the data stream into message segments of appropriate length (usually limited by the maximum transmission unit (MTU) of the data link layer of the network to which the computer is connected). Then TCP transmits the result packet to the IP layer, which transmits the packet to the TCP layer of the receiving entity through the network. In order to ensure no packet loss, TCP gives each byte a sequence number. At the same time, the sequence number also ensures the sequential reception of packets transmitted to the receiving entity. Then, the receiving entity sends back a corresponding acknowledgement (ACK) to the successfully received bytes; If the sending entity does not receive an acknowledgement within a reasonable round-trip delay (RTT), the corresponding data (assuming it is lost) will be retransmitted. TCP uses a checksum function to check whether there are errors in the data; The checksum is calculated when sending and receiving.
Firstly, after the TCP connection is established, both sides of the communication can transmit data at the same time. Secondly, it is full duplex; In order to ensure reliability, timeout retransmission and piggyback confirmation mechanism are adopted.
In terms of flow control, sliding window protocol [1] is adopted, which stipulates that unconfirmed packets in the window need to be retransmitted.
In congestion control, slow start algorithm is adopted.
What is UDP?
UDP is the abbreviation of user datagram protocol, and its Chinese name is user packet protocol. It is a connectionless transport layer protocol in OSI reference model, providing transaction oriented simple and unreliable information transmission service. It is the formal specification of IETF RFC 768 and UDP. In the network, it is used to process packets like TCP protocol. In the OSI model, the transport layer, the fourth layer, is the upper layer of the IP protocol. UDP does not provide datagram grouping, assembly and sorting. In other words, after the message is sent, it is impossible to know whether it arrives safely and completely. UDP is used to support network applications that need to transfer data between computers. Many client / server mode network applications, including network video conference system, need to use UDP protocol. UDP protocol has been used for many years since it came out. Although its initial glory has been covered by some similar protocols, even today, UDP is still a very practical and feasible network transport layer protocol.
Like the well-known TCP (transmission control protocol) protocol, UDP protocol is directly at the top level of IP (Internet Protocol). According to OSI (Open Systems Interconnection) reference model, UDP and TCP belong to transport layer protocols.
The main function of UDP protocol is to compress the network data traffic into the form of datagram. A typical datagram is a transmission unit of binary data. The first 8 bytes of each datagram are used to contain header information, and the remaining bytes are used to contain specific transmission data.
The use of TCP and UDP in Android is exactly the same as in Java.
First, let's take a look at the TCP connection. The following figure shows a schematic diagram of the TCP connection:
Is it easy to understand? I won't say much here. Just look at the code! Practice makes true knowledge.
TCP server code:
TCP client code:
Let's look at UDP:
UDP server side code:
UDP client code: