Network Programming Basics of Network Programming

A CS architecture and BS architecture

  Client (client side) <===========> Server (server side)

  Client software send Server software Recv
  operating system Operating system
  Computer hardware <==== Physical medium =====> Computer hardware

2. Principles of Network Communication

  2.1 What is the network

    The significance of the existence of the network is the cross-regional data transmission ==> called communication

    Network = physical link media (network cable, switch ...) + Internet communication protocol

  2.2 What is the Internet communication protocol

    The essence of the Internet is a series of network protocols

    A hardware operating system is installed, and then you can use it normally after installing the software. However, you can only use it by yourself. Like this, everyone has their own machine, but they are isolated from each other.

1

    How can everyone play together

2

    But what is the internet? In fact, the communication between the two computers is the same as the communication between the two people calling (China has many regions, different regions have different dialects, so that all Chinese can understand, everyone speaks Mandarin)3

    Putonghua belongs to the standard of communication between people in China. What if people from two countries communicate?

4

    The problem is that you ca n’t ask a person / computer to master the world ’s languages ​​/ standards, so there is a unified communication standard in the world: English

5

    Conclusion: English has become the unified standard for all people in the world to communicate. If the computer is regarded as a person distributed around the world, then the Internet connecting the two computers is actually

    A series of unified standards, these standards are called Internet protocols, the essence of the Internet is a series of protocols, collectively called 'Internet Protocol' (Internet Protocol Suite).

    The function of the Internet protocol: define how the computer is connected to the internet, and the standard of computer communication connected to the internet.

 Three osi seven layer protocol

  3.1 Classification of seven-layer protocol

    Internet protocol is divided into seven layers of osi or five layers of tcp / ip or four layers of tcp / ip according to different functions

protocol

    Agreement: Specifies the format of data organization. Format: header + data part. For example, to mail something (data) to someone, you need to package it (data), write the recipient's sender, etc. (head) on the outside, and then mail it to the recipient need

    You can get the contents (data) by removing the package (head).

protocol

  3.2 Introduction to the 5-layer protocol

    3.2.1 Physical layer

Physical layer

      The physical layer mainly sends high and low voltages (electrical signals) based on electrical characteristics, high voltage corresponds to number 1, low voltage corresponds to number 0

      A set of physical layer data is called: bit

      Simple electrical signals are meaningless and must be grouped, which involves the data link layer

    3.2.2 Data link layer

      The origin of the data link layer: pure electrical signals 0 and 1 have no meaning, you must specify how many bits of the electrical signal, and what each group means

      The function of the data link layer: defines the grouping of electrical signals

      ethernet protocol

        Rule 1: A group of data is called a data frame

        Rule 2: The data frame is divided into two parts ==> header + data

          The header contains (fixed 18 bytes): send this / source address (6 bytes) and receiver / destination address (6 bytes) and data type (6 bytes) The address is a mac address

          The data contains (minimum 46 bytes, maximum 1500 bytes): the overall content sent from the network layer

          head length + data length = the shortest 64 bytes, the longest 1518 bytes, if it exceeds the maximum limit, send it in fragments

        Rule 3: It is stipulated that all hosts connected to the Internet must have a network card, and each network card is burned with a unique address in the world at the factory.

      mac address

        The source and destination addresses contained in the head are derived from: ethernet stipulates that all devices connected to the Internet must have a network card, and the addresses of the sending end and the receiving end refer to the address of the network card, that is, the mac address

        The length is 48 binary digits, usually represented by 12 hexadecimal digits (the first six digits are the manufacturer's number, the last six digits are the pipeline number)

      broadcast

        Note: Computer communication basically depends on the roar, that is, the working mode of the Ethernet protocol is broadcast

broadcast

    3.2.3 Network layer

      Origin of the network layer: With ethernet, mac address, and broadcast sending methods, computers in the world can communicate with each other.

      The problem is that the worldwide Internet is composed of small LANs that are isolated from each other. If all communications are broadcast using Ethernet, then packets sent by one machine will be received all over the world.

      Network layer

      The above figure shows the function of the network layer: dividing the broadcast domain, each broadcast domain needs to be connected to the outside, there must be a gateway to help the internal computer forward packets to the public network, the gateway and the outside world use the routing protocol to communicate

      The network layer also has several regulations:

        Rule 1: A group of data is called a data packet

        Rule 2: The data frame is divided into two parts ==> header + data

          The header contains: source address and destination address, the address is an IP address

          Data contains: the overall content sent from the transport layer

      IP protocol:

        The protocol that specifies the network address is called the ip protocol. The address it defines is called the ip address. The widely used version of v4 is ipv4. It specifies that the network address is represented by 32-bit binary

        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

        The ip address is divided into two parts, a legal ipv4 address component = ip address / subnet mask address

        Note: The pure 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

      Subnet mask:

        The so-called "subnet mask" is a parameter that indicates the characteristics of the subnet. It is equivalent to an IP address in form, and is also a 32-bit binary number. Its network part is all 1, and the host part is all 0.

        For example, if the IP address is 172.16.10.1, if the network part is known to be the first 24 bits and the host part is the last 8 bits, then the subnet mask is 11111111.11111111.11111111.00000000,

        Written in decimal is 255.255.255.0.

 

        Knowing the "subnet mask", we can determine whether any two IP addresses are on the same subnet.

        The method is to perform an AND operation on the two IP addresses and the subnet mask (both digits are 1, the operation result is 1, otherwise 0), and then compare the results are the same, if it is, it means that they are in the same sub In the network, otherwise it is not.

# First installed for the binary, and bit-wise comparison is 1 is 1 
computer 1:
 172.16.10.1: 10101100.00010000.00001010.000000001 
255255.255.255.0: 11111111.11111111.11111111.000000000 
172.16.10.0: 10101100.00010000.00001010.000000000 

Computer 2:
 172.16.10.2: 10101100.00010000.00001010.000000010 
255.255.255.255.0: 11111111.11111111.11111111.000000000 
172.16.10.0: 10101100.00010000.00001010.000000000
Case

 

        To sum up, there are two main roles of the IP protocol, one is to assign an IP address to each computer, and the other is to determine which addresses are on the same subnet.

       ARP protocol

        The origin of the arp protocol: computer communication basically depends on the roar, that is, the broadcast method. 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, we learned

        Communication is based on the broadcast method of mac. When the computer sends a packet, it is easy to obtain its own mac. How to obtain the mac of the target host requires the arp protocol

        arp protocol function: send packets in broadcast mode to get the mac address of the target host

 

        Protocol working mode: each host IP is known

        For example: host 172.16.10.10/24 access 172.16.10.11/24

        One: 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: Analysis 172.16.10.10/24 and 172.16.10.11/24 are in the same network (if they are not the same network, then the target ip in the table below is 172.16.10.1, the gateway mac is obtained through arp)

  Source mac Target mac Source ip Target ip Data section
Sender host Sender mac FF: FF: FF: FF: FF: FF 172.16.10.10/24 172.16.10.11/24 data

        Three: This packet will be transmitted in the self-network of the sending end in a broadcast manner. After all hosts receive the packet and unpack it, if the target ip is its own, it will respond and return to its mac.

 

Guess you like

Origin www.cnblogs.com/bk134/p/12714016.html