What is the Network

We surf the Internet every day, the word of the network is certainly not strange, we can not leave the network in our life now, we use wechat and other chat software, if there is no network that can only chat with their own……

How can we chat happily together? That’s the Internet, the Internet

But what is the Internet?

To take a simple example: if a person and their phone were a computer, how would that person communicate with other people? To make a phone call between two people, the first thing must be connected to the telephone line, which is just like the communication between computers, there must be physical link media, such as network cables, switches, routers and other network equipment.

After the communication line is built, there is only a physical layer that can carry data. In order to communicate, we also need to organize our data according to certain rules, so that the other party can parse out the data according to the same rules after receiving the data. The rules here refer to: China has many regions, different regions have different dialects, in order to the whole Chinese people can understand, we speak Putonghua

Mandarin is the standard for communication between people in China, but what about communication between people from two countries?

Shouldn’t we also have two countries speaking one language, so there is a universal communication standard: English

English has become the unified standard for all people in the world to communicate, and the communication between computers should also have a communication standard like English, which is called Internet Protocol. It can be clearly said that Internet protocol is the English of the computer community, and the network is the physical link medium + Internet protocol.

Internet protocol

The term agreement is not only limited to the scope of the Internet, but also reflected in daily life. For example, couples agree to have dinner at a certain place. This agreement is also a kind of agreement. Note that an agreement between one person and oneself cannot be an agreement. The prerequisite of an agreement must be a multi-party agreement.

So what is the network protocol?

Network protocols are the norms for transmitting and managing information in networks (including the Internet). Just as people need to follow certain rules to communicate with each other, computers need to follow certain rules to communicate with each other, these rules are called network protocols.

The Internet without network protocol is chaotic, just like the human society, people can not want to do what they like, your behavior is constrained by the law; Then the end system in the Internet can not send what they want to send, but also need to be constrained by the communication protocol.

Internet protocols are divided into OSI 7 or TCP/IP 5 or TCP/IP 4 (we only need to master TCP/IP 5) according to their functions. This layering is like several stages of learning, and each stage should master specific skills or perform specific tasks

What is the TCP/IP?

TCP/IP protocol is the protocol we programmers contact the most, in fact, TCP/IP is also known as TCP/IP protocol cluster, it does not specifically refer to pure TCP and IP protocol, but to accommodate many network protocols.

However, TCP/IP protocol is not a standard formulated by an international official organization, but a standard negotiated by non-governmental organizations (some large international manufacturers, universities). Because it is easier and more widely promoted, it has become a de facto standard.

TCP/IP five-layer model

To provide a structure for the design of network protocols, network designers organize protocols in a hierarchical manner, with each protocol belonging to one of the hierarchical models, each layer providing services upward.

The OSI model has seven layers, including the physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer. But this is obviously a bit complicated.

We will application layer, presentation layer, session layer and make application layer, from the TCP/IP five layer protocol point of view to elaborate the origin and function of each layer, make clear the main protocol of each layer, understand the principle of the whole Internet communication.

The physical layer

What is the first thing a computer does to communicate with another computer? Of course, we need to connect this computer to other computers so that we can transmit data. For example, they can be connected by optical fiber, cable, twisted-pair and other media, and then they can communicate.

The physical layer protocols use link layer protocols that are related to the actual physical transport media

Physical layer functions: mainly based on electrical characteristics to send high and low voltage (electrical signals), high voltage corresponding to the number 1, low voltage corresponding to the number 0

Data link layer

Pure electrical signal 0 and 1 have no meaning, we must specify how many bits of electrical signal in a group, what is the meaning of each group

Functions of data link layer: it defines the grouping mode of electrical signals

Ethernet protocol: in the early days, each company had its own grouping methods, and then formed a unified standard, namely Ethernet protocol Ethernet

Ethernet rules

  • A group of electrical signals constitutes a packet, called a frame
  • Each data frame is divided into two parts: header head and data 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)

Ethernet provides that devices that access the Internet must have a network adapter. The address of the sending end and receiving end refers to the address of the network adapter, that is, the MAC address

MAC Address: Each nic is delivered with a unique MAC address

If PC1 wants to send data to PC4, it sends Ethernet packets to PC4 in broadcast mode. However, BOTH PC3 and PC2 receive the packets and discards them if the destination MAC address is not their own. If it is their own MAC address, they respond

Broadcast: With a MAC address, two hosts on the same network can communicate with each other

The network layer

Network layer origin: with Ethernet, MAC address, broadcast sending way, the computer in the world can communicate with each other, the problem is that the worldwide Internet is by

Small, isolated lans, so if all communications were broadcast over Ethernet, packets sent by one machine would be sent to the whole world, which would be not just inefficient, it would be a disaster

A way must be found to tell which computers belong to the same broadcast domain and which do not, and if so, broadcast them and if not, route them

Network layer function: a new set of addresses is introduced to distinguish different broadcast domains/subnets. These addresses are network addresses

IP:

The protocol that defines the network address is called IP protocol, and the address it defines is called IP address. Ipv4, which is widely used in V4, defines the network address in 32-bit base 2 and ranges from 0.0.0.0 to 255.255.255.255

IP address classification: Based on the network ID, IP addresses are classified into five types: Class A, Class B, class C, class D, and class E.

Subnet mask

The so-called “subnet mask” is a parameter that represents the characteristics of a sub-network. Formally equivalent to an IP address, it is also a 32-bit binary number with all 1s in the network part and all 0s in the host part.

A subnet mask is used to identify which bits of an IP address represent the network bits and which bits represent the host bits. A subnet mask cannot exist alone. It must be used together with an IP address. A subnet mask is used to divide an IP address into a network address and a host address.

What do I do to distinguish between network bits and host bits?

This is like sending a letter, you send a letter to your southern girl, her body in Xiamen, the detailed address is Xiamen Gulangyu third street 27, that network bit is equivalent to the city, the detailed address is the host bit, network bit to help you locate the city, the host bit to help you find your southern girl. A router uses a subnet mask to determine which bits are network and which are host

IP Address Classification

According to the network ID, IP addresses are classified into five types: Class A, CLASS B, class C, class D, and class E.

Class A IP address: A Class A IP address consists of A 1-byte network address and A 3-byte host address. The highest bit of the network address must be 0. The IP address ranges from 1.0.0.0 to 126.0.0.0. There are 126 Class A networks available, each of which can accommodate more than 100 million hosts.

Class B IP address: A Class B IP address consists of a two-byte network address and a two-byte host address. The highest bit of the network address must be 10. The IP address ranges from 128.0.0.0 to 191.255.255.255. There are 16,382 Class B networks available, each of which can accommodate more than 60,000 hosts.

Class C IP address: A Class C IP address consists of a 3-byte network address and a 1-byte host address. The highest bit of the network address must be 110. The range is 192.0.0.0 to 223.255.255.255. The number of class C networks can reach 2.09 million, and each network can accommodate 254 hosts.

Class D addresses are used for Multicast: A Class D IP address starts with lll0 as the first byte. It is a reserved address. It does not point to a specific network, which is currently used for Multicast. Multicast addresses are used to address a group of computers at a time and identify a group of computers that share the same protocol.

Class E IP addresses start with llll0 and are reserved for future use.

The transport layer

The origin of the transmission layer: the IP of the network layer helps us distinguish subnets, the MAC of the Ethernet layer helps us find the host, and then all of us use are applications, your computer may open QQ at the same time, storm sound, thunder and other applications.

So if we find a particular host by IP and MAC, how do we identify the applications on that host? The answer is the port, which is the number the application is associated with the network card.

Transport layer function: establish port – to – port communication

Added: Ports ranging from 0 to 65535 and 0 to 1023 are used by the system

There are two protocols at the transport layer, TCP and UDP

  • TCP is a connection-oriented, reliable streaming protocol. A stream is an uninterrupted data structure. When an application uses TCP to send messages, the sequence can be guaranteed, but it is still like a data stream without any interval being sent to the receiver. TCP implements sequence control or retransmission control to provide reliable transmission. In addition, it has many functions such as “flow control”, “congestion control” and improving network utilization.
  • UDP is an unreliable datagram protocol. It’s going to hand over the subtlety to the upper application. In the case of UDP, while you can ensure the size of a message sent, you can’t guarantee that it will arrive. As a result, the application sometimes performs retransmission processing as needed.
  • The advantages and disadvantages of TCP and UDP cannot be simply and absolutely compared: TCP is used when reliable transport is necessary at the transport layer; On the one hand, UDP is mainly used for communications or broadcast communications that require high speed transmission and real-time performance. TCP and UDP should be used on demand according to the purpose of application

TCP/IP five layer model explanation: refer to article

The Socket is introduced

We already know that if I want to write a program to send data to another computer, I have to use TCP/IP. But what is the implementation process? What can I do to encapsulate the data into A TCP/IP packet and what instructions can I execute to send the data to the peer machine?

At this point, socket grand appearance, socket is an abstraction layer, applications through it to send and receive data, the use of socket can be added to the application of the network, and in the same network in other applications for communication.

Simply put, Socket provides a port for the program to communicate with the outside world and provides a data transmission channel for the communication parties

What is a socket?

A socket is used by an application program to send or answer requests to the network, enabling communication between hosts or processes on a computer. In plain English, a socket is a ‘phone’ installed in the home of two nodes to communicate with each other.

We know that data is transferred between two computers through a network card. Who owns the network card? Operating system. So I write the program, as long as the data to the operating system, the operating system to call the network card to send.

How does my program give data to the operating system?

Recall that my program needs to open a file stored on the hard disk. Should I just call file? Is file an interface between my program and the operating system?

Similarly, socket is also such an interface, used between the program and the operating system, network data transmission interface. In procedural languages, a socket is a function; in object-oriented languages, a socket is a class, either way, an interface between a program and the operating system.

If TCP is specified, the socket uses TCP to communicate with the peer party. If UDP is specified, the socket uses UDP to communicate with the peer party

UDP send and receive procedures

UDP sending data

import socket def main(): Socket = socket(socket.AF_INET,socket.SOCK_DGRAM) Udp_socket. Sendto (b 'nihao, (8080)' 192.168.0.162 ') # close socket udp_socket. Close () if __name__ = = "__main__ ': the main ()Copy the code

The network debugger receives data

Sends arbitrary data to the network debugger, optimizer

import socket

def main():
    udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    
    while True:
        send_data = input("请输入要发送的数据:")
        if send_data == 'q':
            break
        # 字符串转字节
        send_data = send_data.encode('utf-8')
    
        udp_socket.sendto(send_data,('192.168.0.162',8080))
    
    # 关闭连接
    udp_socket.close()

if __name__ == '__main__':
    main()
Copy the code

UDP receiving data

  • 1 Create a socket
  • 2 Binding Local Information (IP address and Port number)
  • 3 Data acceptance
  • 4 Printing Data
  • 5 Close the socket
import socket def main(): Socket (socket.af_inet, socket.sock_dgram) Udp_socket. Bind (local_addr) while True: local_addr = ('', 7789) # Recv_data = udp_socket. Recvfrom (1024) # 1024 indicates the maximum number of bytes received. Print (recv_data) recv = recv_data[0] send_addr = recv_data[1 print("%s:%s"%(str(send_addr),recv.decode('utf-8'))) print("%s:%s"%(str(send_addr),recv.decode('gbk'))) udp_socket.close() if __name__ == '__main__': main()Copy the code

UDP chat device

import socket def send_data(udp_socket): """ First finish sending data, test """ send_data = input(" Please enter data to send: ") dest_port = int(input(" Please input the port to send :")) # udp_socket.sendto(send_data.encode('utf-8'), (dest_ip, dest_port)) def recv_data(udp_socket): Print ("%s:%s" % (recv_data[1], recv_data[0].decode())) def main(): Socket = socket.socket(socket.af_inet, socket.sock_dgram) udp_socket.bind(("",7788)) while True: send_data(udp_socket) recv_data(udp_socket) if __name__ == '__main__': main()Copy the code

TCP sends and receives data

TCP is divided into client and server

  • A client is a program that runs on a user’s device
  • A server is a program running on a server device that provides data services for clients

TCP client construction process

  • 1. Create a socket
  • 2. Connect to the server
  • 3. Receive/send data (Max. 1024 bytes)
  • 4. Close the socket
import socket def main(): tcp_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # server_ip = input("server ip:") # server_port = int(input("server port:")) # server_addr = (server_ip,server_port) # tcp_client.connect(server_addr) Tcp_client.connect (("192.168.0.162",8080)) send_data = input(" sent data :") # Tcp_client. send(send_data.encode()) recvData = tcp_client.recv(1024) print( Utf-8 tcp_client.close() if __name__ == '__main__': main()Copy the code

TCP server

If you want to make it easier for others to get through to us, you need to do the following things:

  • 1 Buy a mobile phone
  • 2. Plug in your phone card
  • 3 Set the phone to the normal answering state
  • 4 Waiting for someone to make a call

Corresponding Socket flow

  • 1 Socket Creates a socket
  • 2 bind IP and port (can’t ask the client to ask each time)
  • 3 LISTEN enables a socket to be linkable passively
  • 4 Accept The link waiting for the client
  • 5 RecV /send Receives and sends data
import socket def main(): Socket (socket.af_inet, socket.sock_stream) # tcp_server.bind(("",7890)) # Tcp_server.listen (128) # The linker waiting for the client will block # new_client_socket New_client_socket, client_ADDR = tcp_server.accept() # IP address and port of the client Print (client_addr) # Accept data sent from client # Apply to both connection-oriented and connectionless sockets for recvFROM. # recv is used only for connection-oriented sockets and is almost the same as recvfrom, Recv (1024) print(recv_data) # return data to client new_client_socket.send("haha".encode(' utF-8 ')) # Close () tcp_server.close() if __name__ == '__main__': main()Copy the code

The loop serves multiple clients

import socket def main(): Tcp_server = socket.socket(socket.af_inet, socket.sock_stream) tcp_server.bind(("",7891)) # 1 While True: print(" Wait for a new client to connect to.....") Listen (128) # Wait for the client's connection new_client_socket, client_ADDR = tcp_server.accept() # IP and port of the client print(client_addr) # 2. Loop to serve a client multiple times while True: Recv_data = new_client_socket.recv(1024) print(recv_data.decode(' GBK ')) If recv_data: # return data to client new_client_socket.send("haha". Encode (' utF-8 ')) else: New_client_socket.close () print(" service done ") # If the listening socket is closed, it will not be able to wait for a new client to arrive tcp_server.close() if __name__ == '__main__': main()Copy the code

socketserver

The above example can serve multiple clients, but it cannot serve multiple clients at the same time. Is there any solution to this problem?

The socketServer module can be used to implement concurrency for multiple client servers

import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): print(self.request) conn = self.request while True: Conn. Send (b 'hello') print (conn. Recv (1024)) server. = socketserver ThreadingTCPServer ((' 192.168.100.158, 9003), MyServer) server.serve_forever()Copy the code

This is a simplified version of SocketServer concurrency, and we can refine the code a bit

Import socketserver # customize a process communication class, inheritance BaseRequestHandler class MyRequestHandle (socketserver. BaseRequestHandler) : Def handle(self) def handle(self): # if TCP, self.request => conn # if TCP, self. address => client_addr while True: try: msg = self.request.recv(1024) if len(msg) == 0: break self.request.send(msg.upper()) except Exception: Break the self. The request. The close () s = socketserver. ThreadingTCPServer ((' 127.0.0.1, 8889), MyRequestHandle) # server_forever () Repeat the loop until the client requests s.serve_forever()Copy the code

Develop your own static Web server

Implementation steps:

  • Write a TCP server program
  • Obtain HTTP request packet data sent by the browser
  • Read fixed page data, assemble the page data into HTTP response data and send it to the browser.
  • After the HTTP response packet is sent, the socket serving the client is closed.
import socket if __name__ == '__main__': Socket = socket. Socket (socket.AF_INET, socket.SOCK_STREAM) Tcp_server_socket. setsockopt(socket.sol_socket, socket.so_reuseaddr, Tcp_server_socket. bind(("192.168.100.79", 9000)) New_socket, ip_port = tcp_server_socket.accept() Recv_client_data = new_socket.recv(4096) # Decode binary data recv_client_content = recv_client_data.decode(" UTF-8 ") print(recv_client_content) with open("static/index.html", "r") as file: Response_line = "HTTP/1.1 200 OK\r\n" response_line = "HTTP/1.1 200 OK\r\n response_header = "Server: PWS1.0\r\n" # empty line # response_body = file_data # response_data = response_header + "\r\n" + Encode (' utF-8 '))Copy the code

Returns the specified page data

The current Web server, no matter what page the user visits, returns the data of the fixed page, and then needs to return the data of the specified page according to the user’s request

Returns an implementation step for the specified page data:

  • Gets the path to the user requested resource
  • Reads data from the specified file based on the path of the requested resource
  • Assemble a response message for the specified file data and send it to the browser
  • Determine that the requested file does not exist on the server, assemble a 404 status response message, and send it to the browser
import socket def main(): Socket = socket. Socket (socket.AF_INET, socket.SOCK_STREAM) Tcp_server_socket. setsockopt(socket.sol_socket, socket.so_reuseaddr, Tcp_server_socket. bind(("192.168.100.79", 9000)) New_socket, ip_port = tcp_server_socket.accept() Recv_data = new_socket. Recv (4096) # if len(recv_client_data) == 0: Recv_client_content = recv_client_data.decode(" utF-8 ") print(" closed browser ") new_socket.close() # return continue # decode binary data # print(recv_client_content) The maximum number of splits is specified as 2, Maxsplit =2 request_list = recv_client_content.split(" ") print(request_list) # If request_path == "/": request_path = "/index.html" try: With open("static" + request_path, "rb") as file: File_data = file.read() except Exception as e: Response_line = "HTTP/1.1 404 Not Found\r\n" # response_header = "Server: PWS1.0\r\n" with open("static/error.html", "rb") as file: Response_data = file.read() # response_data = file.read() # response_data = file.read() # response_data = file.read() # response_data = file.read() # response_data Encode (" utF-8 ") : encode("utf-8") : encode("utf-8") : # response_line = "HTTP/1.1 200 OK\r\n" # response_line = "Server: PWS1.0\r\n" # response_data = (response_line + response_header + response_data Encode (" utF-8 ") + response_body: New_socket.close () if __name__ == '__main__': main()Copy the code