“This is my 32nd day of participating in the First Challenge 2022. For details: First Challenge 2022”

The entire computer network is made up of protocols.

In fact, an agreement can be likened to the language we use as human beings. When we communicate with foreigners, both sides have to speak the same language as if they follow the same “agreement”. The same is true when two machines on a network communicate with each other.

Seven layer network model — OSI standard

The OSI seven-tier model is a standard that specifies how machines (mainly computers) communicate with each other. So if you want your dishwasher and washing machine to communicate, you need to follow, or at least be inspired by, the OSI model. This means complying with a layered approach to communication.

There’s a memorization formula for this seven-tier model: All People Seem To Need Data Processing

It seems that everyone needs data processing. The sentence consists of seven English words, each of which begins with the first letter in the order of the seventh to the first letter.

memory explain
All Application
People Presentation
Seem Session
To Transport
Need Network
Date Datelink
Process Physical

Of course, 👨🎓 standard and actual application is still different, the actual application is the following five layer network model.

Five layer network model

OSI function TCP/IP protocol
The application layer File transfer, E-mail, file services HTTP, FTP, STMTP, DNS, Telmet, etc
The transport layer Provide an end-to-end interface TCP, UDP
The network layer Select a route for the packet IP, ICMP, etc
Data link layer Transmission has address frame, error detection function ARP etc.
The physical layer Physical media Base – 1000 SX, etc

For example, client A — server B transmits data: first through the application layer, down to the transport layer, everything goes down to the network layer, the routing data link layer, Repeaters, routers and finally to the physical layer, network cables or radio waves

Bottom up, data group package, group data –> string or JSON data,

We use application layer protocols the most,

According to the IP address to the Web server –> operating system –> Port 80 –>Ngix–>UWSGI

Resolve address -DNS, query IP protocol by domain name. Browsers implement the HTTP protocol

The chat protocol is different from HTTP protocol. If you directly deal with the underlying protocol, the programming is complicated

The operating system provides us with sockets, an API for dealing directly with the transport layer.

To achieve their own application functions, HTTP protocol one-way, Socket programming significance. The Socket itself is not a protocol, but a tool for connecting applications to interact with TCP/UDP to implement its own protocol.

The Client communicates with the Server

The connection between the TCP server and client is shown as follows:

Python Socket programming

Simple communication:

  1. Creating a serverserver_socket.py, bind local IP, port 9999, and then perform service listening, and then wait for the client to send data, and print the data received from the client.
# server_socket.py
import socket


Create a socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Bind IP and port, tuple storage address and port
server.bind(('0.0.0.0'.9999))
# server listener
server.listen()
# accept Accepts the request from the user
sock, addr = server.accept()
Receive 1K of data at a time
data = sock.recv(1024)
Print the received data
print(data.decode('utf8'))
# Close the server connection
server.close()
Copy the code
  1. Creating a Clientclient_socket.py, to local IP, port 9999
# client_socket.py
import socket


Create a socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The server to connect to
client.connect(('127.0.0.1'.9999))
# Data to send
client.send('Hello, Server'.encode('utf8'))
# close the connection
client.close()
Copy the code

Run the server program, then allow the client program, and then go back to the server console and see the following output:

Two-way communication:

# socket_server.py
import socket


Create a socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Bind IP and port, tuple storage address and port
server.bind(('0.0.0.0'.8888))
# server listener
server.listen()
# accept Accepts the request from the user
sock, addr = server.accept()
# Loop to send and receive messages
while True:
    Receive 1K of data at a time
    data = sock.recv(1024)
    Print the data received by the client
    print('Client :', data.decode('utf8'))
    The server sends data to the client
    re_data = input('Server :')
    sock.send(re_data.encode('utf8'))
Copy the code
# client_socket.py
import socket


Create a socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The server to connect to
client.connect(('127.0.0.1'.8888))
while True:
    # Data to send
    re_data = input('Client :')
    client.send(re_data.encode('utf8'))
    # Data to receive
    data = client.recv(1024)
    print('Server :', data.decode('utf8'))
Copy the code

Once again, we run the server code socket_server.py, followed by the client client_socket.py

  1. The client sends a message to the server: Hello server.
  2. When the server receives the message, it replies with a Hello, client
  3. Client to server: Let’s chat

The demo is as follows:

Through this process, the communication between a single client and the server is realized. If a server program wants to interact with multiple clients at the same time, it needs to take advantage of multithreaded programming, which is not discussed here.

Socket programming is an important part of network programming, to master the premise of asynchronous IO and coroutines, there are more content about Socket programming to learn.

TCP/IP Volume 1: Protocols