TCP/IP protocol, which is the lower layer of HTTP protocol, is responsible for specific data transmission work, it is very classic, and it is the de facto network communication standard

TCP/IP network layered model

TCP/IP is a layered protocol to divide the complex network communication into multiple levels, and then assign different responsibilities to each level, within the level only concentrate on doing their own things, with the idea of “divide and rule” to split the problem, so as to solve the problem of network communication.

TCP/IP has a total of layers: the link layer, the Internet layer, the data transmission layer, and the application layer

  • Link layer: The link layer is responsible for sending raw packets on the underlying network such as Ethernet and WiFi. It works at the nic level and uses MAC addresses to mark devices on the network, so it is sometimes called MAC layer.
  • Internet Layer: The IP protocol is located at this layer. Because IP protocol defines the concept of “IP address”, it can replace MAC address with IP address on the basis of “link layer”, and connect many Lans and wide area networks into a virtual huge network. When looking for devices in this network, it is ok to “translate” IP address into MAC address again.
  • Transport Layer: This layer is responsible for ensuring the “reliable” transfer of data between two points marked by IP addresses. This layer is where TCP works
  • Application layer: There are various application-specific protocols. Examples include Telnet, SSH, FTP, SMTP, and of course the most common HTTP

The transmission unit at the MAC layer is frame, the transmission unit at the IP layer is packet, the transmission unit at the TCP layer is segment, and the transmission unit at HTTP is message. But there is no essential distinction between these terms, which can be collectively referred to as data packets.

OSI reference model

The seven-layer model we often hear about is the concept of the OSI, which divides the network into seven layers, namely the physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer from bottom to top.

Difference between TCP and OSI

The TCP/IP application layer corresponds to the OSI application layer, presentation layer and session layer. The TCP/IP network interface layer corresponds to the OSI data link layer and physical layer

TCP/IP details

The application layer

We tried to connect to Baidu

nc www.baidu.com 80
Copy the code

If you look at it, it’s connected

Connect after we as a user to do what, request Baidu home page is the application layer HTTP protocol, you can try to write, in the connection below enter GET/HTTP/1.0 (note that there should be a newline at the end), baidu home page content

In the application layer, we want to communicate with Baidu server to obtain the page, establish a connection first, and then use the protocol to send a request, which is also simulated by the browser to enter Baidu enter operation

  • Nc www.baid.com 80 Establish a connection
  • GET/HTTP/1.0 follows the HTTP protocol to send requests.

The transport layer

Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream based transport layer communication Protocol. The life cycle of a complete TCP connection consists of three phases: Establishing a connection, transmitting data, and disconnecting a connection.

Three-way handshake

TCP establishes a connection through the three-way handshake. The ESTABLISHED state is ESTABLISHED. There’s this three-way handshake so TCP is reliable,

Proto Recv -q send-q Local Address Foreign Address State PID/Program name TCP 0 0 172.17.0.2:55794 36.152.44.95:80 ESTABLISHED 106/ncCopy the code

When the two parties establish a connection, the two parties will initiate resource switchover. Resources and resources are corresponding to each other

So if a program wants to communicate with the outside world, it first needs to establish a connection by listening to the TCP three-way handshake at the transport layer

There are several nouns

Socket: a socket that generates another quad (source IP +port destination IP +port mapping, absolutely unique connection)

Port: Specifies the restricted port number provided by the kernel. A system can allocate a maximum of 65535 port numbers

Break up four times

The four break-ups involve connections, which are actually resources on both the server and the client

What happens if the connection is established and the network is disconnected and the breakup fails,

If you connect to Baidu at home, the backbone network in the middle breaks down, both sides actually do not know that the other side is disconnected, both sides have no physical connection, no conditional reflex; Only memory opens up resources and is decoupled from the upper layer, that is, the application layer is decoupled, although the application is not aware of it, but the kernel will have heartbeat check (keeplive); The kernel’s heartbeat checks whether the current quad is healthy, that is, the current connection is healthy; Of course, the application can also do a heartbeat, which checks the health of the application space role

Caught validation

Request Baidu through the following command, do not look down upon this command, execution of it includes the establishment of a connection, request data, disconnect, exit program completion process

curl www.baidu.com:80
Copy the code

Again through the following command to capture the packet

tcpdump -nn -i eth0  port 80
Copy the code

  • 1, local random port number and baidu packet syn (transport control layer)
  • 2. The server replies with an ACK
  • The local server replies with an ACK

The length of the packet above is 0 because it does not involve data transmission; Look at the bag below

  • 4. A local get request with length=77 was sent
  • Baidu first back to an ACK
  • 6. Immediately return a packet of 1461 size
  • 7. Then we return an ACK
  • 8, Baidu returned a packet with length=1321 (the returned data is large, so it is divided into two packets)
  • 2782=1460+1321
  • 10, the back of the four times to break up, here is not detailed, see….

With a wave of practice, logic and theory match up

The network layer

The Internet layer is mainly to solve the process specification of how to route data from the IP of a computer to the target computer. After our computer message is sent out, we can find the target computer correctly through what processing, including IP, ARP, RARP, ICMP and other protocols.

The MAC address

Our message if you want to in the network to find the corresponding target computer, so our computer must have a unique address identifier, and can distinguish between computer only our logo is our network adapter address, our network adapter address is prescribed by the manufacturer when the strings of a complex English.

The IP address

Due to the complexity of the MAC address is a string of logical operations against our computer, and our message to expressway by to the target computer in the network, then must need to have a set of logical operation rules, and so have the ARP, ARP protocol specifies how to convert the MAC address to digital IP address can be used for a logic operation.

The domain name

Because IP addresses are too long to be memorized, the DNS service is derived to make it easier for users to remember. The DNS service converts IP addresses into simple and easy to remember domain names.

The link layer

The link layer mainly manages the data required for physical network preparation, including

  • 1. Connect to the computer network adapter.
  • 2, according to the appropriate way to adjust the data transmission (different transmission media and network format is different)
  • 3, the data into the form of electron flow or pulse transmission on the transmission medium.
  • 4. Add error check information to the sent data and check the received data.

Except your body triple snow, the world who match white clothes