Preface:

  • Proficient in TCP/IP, proficient in network programming using Socket.

See this sentence, do you feel very familiar? I believe many of you have seen this requirement when sending out resumes. Many of you will think that we generally do not use this knowledge in the actual development, so they will not look at these things.

However, I believe that in order to make a better APP, you must have a certain understanding of these basic knowledge, so that you can consider more comprehensive and complete, let’s take a look at what TCP/IP is.

1. What is TCP/IP?

TCP/IP is a protocol system. It is a set of protocols used for network communication.
TCP/IP is traditionally considered a layer 4 protocol

1) Network interface layer:

This refers to interfaces at the physical level, such as cables.

2) Network layer:

Provides hardware – independent logical addressing to convert physical addresses to logical addresses.

In the TCP/IP protocol family, network layer protocols include IP (Internet Protocol), ICMP (Internet Control Message Protocol), and IGMP (Internet Group Management Protocol).

3) Transmission layer:

Provides traffic control, error control and validation services for the network.

There are two distinct transport protocols in the TCP/IP protocol family: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).

4) Application layer:

Provides specific applications for network troubleshooting, file transfer, remote control and Internet operations

2. The packet

In TCP/IP, data is packed from top to bottom and then unpacked from bottom to top

During packet loading, each layer adds some information for transmission. This part of information is called the header. When the data from the upper layer arrives at this layer, the data is packaged together with the header of this layer and continues to be transmitted.

To pack

When unpacking, each layer reads the headers it needs and uploads the rest of the data.

The process is a bit like a Russian nesting doll, so people sometimes refer to the process as a Russian nesting doll.

Russian nesting doll

3. Network interface layer

This area mainly involves some physical transmission, such as Ethernet, wireless LAN. I won’t go into details here

4. The network layer

As mentioned earlier, the network layer mainly does the translation between the physical address and the logical address.

At present, 32-bit binary IPv4 is the most widely used in the market, because IPv4 addresses are not enough, so 128-bit binary IPv6 is more and more widely used (but the following is based on IPv4).

1) IP:

Each network adapter on a TCP/IP network has a unique IP address.

An IP address is a 32-bit address, which is usually divided into four bits and eight bits. However, each bit is converted to decimal for easy reading, such as 192.168.0.1

IP addresses are divided into two parts:

  • Network ID
  • The host ID

However, it is not specified which part belongs to the network ID and which belongs to the host ID.

Because some networks require many hosts, the host ID portion should be larger, but some networks require fewer hosts, so the host ID portion should be smaller.

Most IP addresses fall into the following categories

  • Class A ADDRESS: The first eight bits of an IP address represent the network ID, and the last 24 bits represent the host ID.
  • Class B address: The first 16 bits of an IP address represent the network ID and the last 16 bits represent the host ID.
  • Class C ADDRESS: The first 24 bits of an IP address represent the network ID and the last 8 bits represent the host ID.

It is obvious that class A addresses can provide fewer network ids, but each network can have A large number of hosts

But how do we know what kind of address an IP address really is?

  • If A 32-bit IP address starts with 0, it is A class A address.
  • If a 32-bit IP address starts with 10, it is a class B address.
  • If a 32-bit IP address starts with 110, it is a class C address.

In decimal notation, we can use the decimal number in the first paragraph to distinguish what kind of IP address is.

Note:

  • The first decimal segment greater than 223 belongs to class D and E addresses, which are special and uncommon and won’t be covered in detail here.
  • Each class has a set of excluded addresses that do not belong to the class and are used in special cases (described below).
  • Instead of dividing the network in this way, we can divide each network into smaller network blocks called subnets (described later)

Host IDS with all zeros represent the network itself. For example, IP address 130.100.0.0 refers to class B addresses with network ID 130.100.

Host ids with all 1s represent broadcasts, which are used to send messages to all hosts in the network. The IP address 130.100.255.255 is the broadcast address of the network whose ID is 130.100.

Addresses starting with decimal 127 are loopback addresses. Messages whose destination address is a loopback address are actually sent and received locally. It is used to test whether TCP/IP software works properly. When using ping, the usual loopback address is 127.0.0.1

2) Address resolution protocol ARP

In simple terms, ARP maps IP addresses to physical addresses, while RARP maps physical addresses to IP addresses.

3) subnet

The classification of IP addresses was mentioned earlier, but for class A and CLASS B addresses, the number of hosts under each network is too large, and network transmission becomes inefficient and inflexible. For example, the class A IP address is 100.0.0.0. There are more than 16 million hosts in this network.

So subnet masks were invented to solve this problem.

Let’s review how we distinguished host IP from network IP.

Take class A address 99.10.10.10 as an example. The first eight bits are network IP addresses, and the last 24 bits are host IP addresses. (As shown below)


The subnet mask is also a 32-bit binary number, or can be divided into four decimal numbers. Each bit of the subnet mask corresponds to the corresponding position of the IP address. The value 1 indicates the non-host bit, and the value 0 indicates the host bit.

It can be clearly seen from the table that the number of bits of network IP is still determined by the previous classification, while the number of bits of host IP is determined by the number of bits whose subnet mask value is 0, and the rest are subnet IP

5 the transport layer

The transport layer provides two ways to reach the target network

  • Transmission control Protocol (TCP) : provides complete error control and traffic control to ensure normal data transmission. It is a connection-oriented protocol.
  • User Datagram Protocol (UDP) : provides only basic error detection and is a connectionless protocol.

Features:

1) the UDP:

  • Package the data
  • Limited data size (64K)
  • Not establishing a connection
  • Fast, but low reliability

2) TCP:

  • Establishing a connection channel
  • Unlimited data size
  • Slow speed but high reliability

Because the transmission layer involves a lot of things, such as ports, sockets, etc., we need to understand mobile development, we will make a specific introduction in the following article, here will not explain.

6 the application layer

Application layer as the highest layer of TCP/IP protocol, for our mobile development, is the most contact.

Protocols running over TCP:

  • Hypertext Transfer Protocol (HTTP) is used for common browsing.
  • Hypertext Transfer Protocol over Secure Socket Layer (HTTPS), a Secure version of HTTP.
  • File Transfer Protocol (FTP) is used for File Transfer.
  • POP3 (Post Office Protocol, Version 3) is used to receive mails.
  • SMTP (Simple Mail Transfer Protocol) is used to send emails.
  • TELNET (Teletype over the Network) is a method of logging in to the Network through a terminal.
  • Secure Shell (SSH) is used to encrypt and Secure login.

    Protocols running on UDP:

  • Boot Protocol (BOOTP) : applies to diskless devices.
  • Network Time Protocol (NTP) is used for Network synchronization.
  • Dynamic Host Configuration Protocol (DHCP) is used to dynamically configure IP addresses.

    Other:

  • The Domain Name Service (DNS) is used to search IP addresses and forward mails (running over TCP and UDP).
  • ECHO (ECHO Protocol), used to check errors and measure response time (running over TCP and UDP).
  • Simple Network Management Protocol (SNMP) is used to collect Network information and manage networks.
  • Address Resolution Protocol (ARP) is used to dynamically resolve Ethernet hardware addresses.

Similarly, because of the application layer we need to involve too many things, we will introduce the specific introduction in the later article, this article will not be extended, interested friends can continue to follow my blog:

blog.csdn.net/yulyu

Popular articles

  • Glide- source code in detail
  • Progressive loading – Basics explained
  • Use productFlavors
  • OnTouch event delivery
  • The pits we ran into in the years of sliding conflicts
  • Interprocess communication –AIDL
  • Serialization –Serializable vs. Parcelable
  • How to solve memory overflow and leak
  • Okhttputils Ultimate encapsulation
  • FaceBook debugger
  • Android code optimization tool
  • Glide- An introductory course
  • Glide- Image preprocessing (fillet, Gaussian blur, etc.)
  • Glide- Compression of pictures
  • Glide- Memory cache and disk cache
  • Glide- Custom cache