Network programming socket, detailing the seven-layer osi protocol

a network programming

Source:

Let's first talk about the transfer of data between two computers:

For users, we usually send data at the application layer, that is, we operate the application software directly, then the application layer needs to package the data to transmit the data to the transport layer, which is equivalent to giving him a data header , wrapped in an envelope for easy identification. Then the application layer does not have to wrap this layer, we can customize it, but it is generally best to wrap it.

Next, enter the transport layer: the transport layer wraps another layer on the outside. This layer is what we call the source port and the destination port.

Then go down to the network layer: the network layer will further process the data, that is, wrap a layer of headers outside it, and wrap the source IP and destination IP.

Further down is the data link layer: that is, Ethernet, Ethernet. At this time, the outermost leak is the source mac and the target mac.

Physical layer: Break up data into binary, that is, electrical signals,

Target party:

Unpack it and find that the target mac is yourself, unpack it and leak the ip, and then unpack the port.

 

Details: http://www.cnblogs.com/linhaifeng/articles/5937962.html

  1 Operating system: (Operating System, referred to as OS) is a computer program that manages and controls computer hardware and software resources. It is the most basic system software that runs directly on "bare metal". Any other software must be supported by the operating system. to run.

Note: computer (hardware) -> os -> application software

 

First introduce the physical layer:

Connect the client and the server through a network cable or optical fiber, through the Internet,

Physical connection medium: twisted pair, radio waves, optical cable, optical fiber.

What the physical layer does is to program the data binary to send the electrical signal 01010100010101,

Then a simple electrical signal is meaningless, that is to say, another machine cannot recognize a bunch of binary signals, so there should be a unified form.

data link layer:

To group data, there must be a unified standard. This standard is: ethernet, which is the Ethernet protocol.

Ethernet protocol details:

Ethernet protocol:

In the early days, each company had its own grouping method, and later a unified standard was formed, namely the Ethernet protocol ethernet

ethernet regulations

A group of electrical signals that make up a packet, called a 'frame'
Each data frame is divided into two parts: header head and data data
       head                           data                             
 

head contains: (fixed 18 bytes)

sender/source address, 6 bytes
Receiver/Destination Address, 6 bytes
data type, 6 bytes
data contains: (minimum 46 bytes, maximum 1500 bytes )

The specific content of the packet
head length + data length = minimum 64 bytes, maximum 1518 bytes, if the maximum limit is exceeded, it will be sent in fragments

Then the address in the Ethernet uses the mac address, and the mac address applies to the local area network:

mac address:

The origin of the source and destination addresses contained in the head: ethernet stipulates that all devices connected to the internet must have a network card, and the addresses of the sender and receiver refer to the address of the network card, that is, the mac address

mac address: Each network card is burned with a unique mac address in the world when it leaves the factory. The length is 48 binary digits, usually represented by 12 hexadecimal digits (the first six digits are the manufacturer number, and the last six digits are the pipeline No)

broadcast:

With the mac address, two hosts in the same network can communicate (one host obtains the mac address of the other host through the arp protocol)

ethernet uses the most primitive way to communicate by broadcasting, that is, computer communication basically relies on roaring

Network layer function: introduce a new set of addresses to distinguish different broadcast domains/subnets, this set of addresses is the network address

IP protocol:

  • The protocol that specifies the network address is called the ip protocol, and the address it defines is called the ip address. The widely used v4 version is ipv4, which specifies that the network address is represented by a 32-bit binary system.
  • Range 0.0.0.0-255.255.255.255
  • An ip address is usually written as a four-segment decimal number, for example: 172.16.10.1

ip address is divided into two parts

  • Network Section: Identifying Subnets
  • Host section: Identifies the host

Note: The simple ip address segment only identifies the type of ip address, and the subnet where an ip is located cannot be identified from the network part or the host part.

Example: 172.16.10.1 and 172.16.10.2 cannot be sure that they are in the same subnet

subnet mask

The so-called "subnet mask" is a parameter that represents the characteristics of the subnet. It is formally equivalent to an IP address and is also a 32-bit binary number with all 1s for the network part and all 0s for the host part. For example, for the IP address 172.16.10.1, if it is known that the network part is the first 24 bits and the host part is the last 8 bits, then the subnet mask is 11111111.11111111.11111111.00000000, which is 255.255.255.0 in decimal.

 

ip packet

The ip data packet is also divided into head and data parts. There is no need to define a separate field for the ip packet, and it is directly placed in the data part of the Ethernet packet.

 

head: 20 to 60 bytes in length

data: Up to 65,515 bytes.

The "data" part of the Ethernet packet is only 1500 bytes long. Therefore, if an IP packet exceeds 1500 bytes, it needs to be split into several Ethernet packets and sent separately.

 

ethernet header                ip header                                     ip data                                

 

 

 ARP protocol

The origin of the arp protocol: computer communication basically relies on roar, that is, the way of broadcasting. All upper-layer packets must be encapsulated with an Ethernet header at the end, and then sent through the Ethernet protocol. When talking about the Ethernet protocol, I learned that

Communication is realized by mac-based broadcasting. When a computer sends a packet, it is easy to obtain its own mac. How to obtain the mac of the target host needs to pass the arp protocol.

arp protocol function: send data packets by broadcasting, get the mac address of the target host

 

How the protocol works: every host ip is known

For example: host 172.16.10.10/24 access 172.16.10.11/24

1: First, distinguish the subnet you are in by ip address and subnet mask

Scenes packet address
same subnet target host mac, target host ip
different subnets Gateway mac, target host ip

 

 

 

Two: Analyze that 172.16.10.10/24 and 172.16.10.11/24 are in the same network (if it is not the same network, then the target ip in the table below is 172.16.10.1, and the mac of the gateway is obtained through arp)

  source mac target mac source ip target ip data part
sender host sender mac FF:FF:FF:FF:FF:FF 172.16.10.10/24 172.16.10.11/24 data

 

 

3: This packet will be transmitted in the self-network where the sender is located by broadcasting. All hosts will open the packet after receiving it, and find that the target ip is its own, then respond and return its own mac

 

4 Transport Layer

The origin of the transport layer: the ip of the network layer helps us distinguish the subnet, the mac of the ethernet layer helps us find the host, and then everyone uses the application, and multiple applications such as QQ, Baofengyingyin, etc. may be opened on your computer at the same time. program,

Then we found a specific host through ip and mac, how to identify the application on this host, the answer is the port, the port is the number associated with the application and the network card.

Transport layer function: establishing port-to-port communication

Supplement: The port range is 0-65535, and 0-1023 is the port occupied by the system

tcp protocol:

Reliable transmission, there is no length limit for TCP data packets, which can theoretically be infinitely long, but in order to ensure the efficiency of the network, usually the length of TCP data packets will not exceed the length of IP data packets, so as to ensure that a single TCP data packet does not need to be divided.

ethernet header ip header               tcp header               data                                                    

 

udp protocol:

Unreliable transmission, the "header" part is only 8 bytes in total, and the total length does not exceed 65,535 bytes, which fits into an IP packet.

ethernet header ip header                      udp header                            data                                           

 

 

TCP three-way handshake and four-way wave ヾ(ToT)Bye~Bye~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324791286&siteId=291194637