When we learn and use the Android network programming, we touch only the upper layer protocols and interfaces such as Apache HttpClient or Android's own httpurlconnection and so on. We also need to learn more about the underlying implementations of these interfaces, which requires that we understand the network communication layer and that the network communication layer has to speak of the Iso-osi seven-layer protocol classic architecture:
The left part is the OSI architecture model, the ISO/OSI model, the Open System Interconnect Reference Model (open System Interconnection Reference model), is a standard framework proposed by the International Organization for Standardization (ISO) that attempts to interconnect computers worldwide as a network, referred to as the OSI. At the far right of the graph is the TCP/IP protocol model, the TCP/IP protocol model (transmission Control protocol/internet Protocol), which contains a series of network protocols that form the basis of the Internet (the corresponding intermediate parts). Is the core of the Internet Protocol, through more than 20 years of development has matured, and is widely used in LAN and WAN, has become a de facto international standard. The TCP/IP protocol cluster is a combination of multiple protocols at different levels and is often considered a four-layer protocol system, which corresponds to the OSI seven-tier model. So usually we mention the TCP/IP protocol it does not refer to a specific protocol but rather a generic term for a network transport protocol collection model. Corresponding to the standard OSI model it developed its own four-layer protocol system, each containing several different communication protocols.
So how does TCP/IP work and why does it need it? All over the world, a wide variety of computers run their own different operating systems for everyone, and these computers are used in many ways when expressing the same message. It is as if God in the Bible has disturbed the accents of people everywhere and made them unable to cooperate. Computer users realize that the computer is only a man-in-the-war and does not play a big role. Only by bringing them together can the computer play its greatest potential. So people are trying to connect the computer to each other by electric wires. But the simple connection is not enough, as if the language of the different two people meet each other, completely unable to exchange information. So they need to define something common to communicate, and TCP/IP is born for that.
Let's look at the specific layer of TCP/IP protocol system,
See above hierarchy chart, if you have known before, may read above the socket abstraction layer to express doubts, matter will be introduced later. We can see a total of four layers of TCP/IP layer: Link layer, network layer, Transport layer and application layer, conceptually there is no socket abstraction layer.
(1). LINK Layer
Also known as the data link layer or the Network interface layer (the network interface layer and the hardware layer in the first diagram), typically includes the device drivers in the operating system and the corresponding network interface cards in the computer. They work together with the physical interface details of the cable (or any other transmission medium). ARP (Address Resolution Protocol) and RARP (inverse Address Resolution Protocol) are special protocols used by certain network interfaces, such as Ethernet and Token Ring networks, to transform the addresses used by the IP layer and the network interface layer. Specific details can be transferred to this side of the article "Android Network programming series of a TCP/IP protocol Family link Layer"
(2). Network layer
Also known as the Internet layer (in the first figure as the internetwork), processes the grouping of activities in the network, such as routing of groupings. In the TCP/IP protocol family, the network layer protocol consists of the IP Protocol (Internet Protocol), the ICMP Protocol (Network Internet Control Message Protocol), and the IGMP Protocol (Internet Group Management Protocol).
IP is a network layer protocol that provides an unreliable service that simply sends packets from the source node to the destination node as quickly as possible, but does not provide any reliability guarantees. Both TCP and UDP are used. Each set of data for TCP and UDP is transmitted over the Internet through the end system and the IP layer in each intermediary router.
ICMP is a subordinate protocol to the IP protocol. The IP layer uses it to exchange error messages and other important information with other hosts or routers.
IGMP is an Internet Group Management protocol. It is used to multicast a UDP datagram to multiple hosts.
Specific details can be transferred to this side of the article "Android Network programming Series A TCP/IP protocol family of the Internet layer"
(3). Transport Layer
It mainly provides end-to-end communication for applications on two hosts. In the TCP/IP protocol family, there are two different transport protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).
TCP provides high-reliability data communication for two hosts. The work that it does involves putting the data that the application gives to it into the appropriate small pieces to the following network layer, confirming the received packet, setting the timeout clock to send the last acknowledgment packet, etc. Because the transport layer provides high-reliability end-to-end communication, the application layer can ignore all of these details. In order to provide reliable service, TCP adopts the mechanism of time-out retransmission, sending and receiving end-to-end acknowledgement grouping.
UDP provides a very simple service for the application layer. It only sends packets called datagrams from one host to another, but does not guarantee that the datagram will reach the other end. A datagram is a unit of information (for example, a certain number of bytes specified by the sender) that is transmitted from the sender to the receiving party. Any required reliability of the UDP protocol must be provided by the application layer.
Specific details can be transferred to this side of the article "Android Network programming Series A TCP/IP protocol Family Transport Layer"
(4). Application Layer
The application layer is responsible for handling specific application details.
(5). Socket Abstraction Layer
For the display of the socket abstraction layer this is a very important level, is directly responsible for the application layer and Transport layer communication, its role is to make the upper layer (application layer) communication more convenient and secure. In fact, it is the encapsulation of the transport layer to the Application layer interface, which is now not extended, the next article will focus on the socket abstraction layer
。
Each layer of the TCP/IP protocol system is very clear in the data transmission work, we can see its data communication diagram:
First of all, the application layer is the top level, the user will apply the data through the application layer protocol such as HTTP, FTP, etc. to encapsulate the necessary client data information to the next layer (transport layer), when the Transport layer TCP or UDP protocol will be carried on the client port and other information. Encapsulated and then passed to the internetwork, where the data is carried on the client's IP and other information. And then the data is sent to the link layer, which adds an Ethernet protocol header to the data to be transmitted, and the CRC encodes it to prepare for the final data transfer while also carrying the MAC address of the target host for the data (each Ethernet card MAC address is unique). Then down is the level of hardware, responsible for the transmission of the network, the definition of this level includes the network cable format, the definition of network card and so on.
Understand the data sent, and then take a look at the target host to the data receiving process, such as:
The above mentioned that the client in the transmission of data at the link layer will be the data on the destination host MAC address, all can be based on the data carried by the IP address and MAC address in the Internet to find the destination host, and then when the target host receives an Ethernet data frame, the data starts from the protocol stack from the bottom up, At the same time, remove the header of each layer protocol plus the message. Each layer of the Protocol box is to check the protocol ID in the header to determine the upper layer protocol that receives the data. This process is called splitting (demultiplexing). The protocol is unpacked by the destination port number, the source I p address, and the source port number.
The above is the general process of TCP/IP protocol communication, then in order to better absorb and understand these knowledge points, you need to understand the following basic concepts:
- IP Address
Each node on the network must have a separate Internet address (also known as an IP address). Now, the commonly used IP address is a 32bit number, which is what we often call the IPV4 standard, this 32bit number is divided into four groups, that is, the common 255.255.255.255 style. IPV4 Standard, the address is divided into five categories, we often use a class B address. Please refer to the other documentation for specific classifications. It is important to note that the IP address is a combination of network number + host number.
- Dns
The domain Name system is a distributed database that provides services that convert host names (URLs) to IP addresses. DNS is the abbreviation for a computer domain Name System or domain name Service, which consists of a parser and a domain name server. A domain name server is a domain name and corresponding IP address that holds all the hosts in the network, and a server that converts the domain name to the IP address feature.
- Rfc
What is an RfC? The RFC is the standard document for the TCP/IP protocol, where we can see the long definition list of RFCs, which now has a definition of more than 4,000 protocols, and of course, the more than 10 protocols we are going to learn.
- Port number (port)
Note that this number is a logical number used on the TCP,UDP to differentiate between different applications on the host. Servers are generally identified by well-known port numbers. For example, for each TCP/IP implementation, the TCP port number for the FTP server is 21, and the TCP port number for each Telnet server is 23, and the UDP port number for each TFTP (Simple File Transfer Protocol) server is 69. Any TCP/IP implementation provides services that use the port number between the well-known 1~1023. These well-known port numbers are managed by the Internet Number Distribution Authority (Internet Assigned Numbers Authority, IANA). The well-known port number is between 1~255; the port number between 256~1023 is typically used by UNIX systems to provide specific UNIX services, the 1024~5000 port number is for the client to assign a temporary port number, and the port number greater than 5000 is reserved for other servers.
- Application Programming Interface
Now the common programming interfaces are sockets and Tli. This is the socket abstraction layer mentioned above.
Android Network programming series one TCP/IP protocol family