1. C/S Architecture
1. Hardware C/S Architecture
such as PC-printers
2. Software C/S Architecture
such as PC-web server
reference:
https://baike.baidu.com/item/Client%2FServer/1504488?fr=aladdin&fromid=826311&fromtitle=C%2FS
2. OSI seven-layer model
The seven layers of OSI correspond to various communication protocols in network communication. Understanding the operation process of the seven layers of OSI is the most important part of socket programming. Why do you say this? Look at the picture below:
As can be seen from the figure, the socket works between the software C/S architecture and the application layer and the transport layer in the OSI seven-layer model
Attached:
Detailed explanation of the TCP protocol:
3. What kind of shit is a socket?
Socket originally refers to a hole or socket. It was originally used as a process communication mechanism for BSD UINX, and is also called a socket. Today's socket is a network programming standard that Windows and Mac operating systems abide by. It is an abstraction layer between the application layer and the transport layer in the OSI seven-layer model. It is used to describe IP addresses and ports. It is a handle of a communication chain. It can be used to realize the communication between different virtual machines or different computers or the communication between different processes in the host. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a socket and binds it to a port. Different ports correspond to different services.
On the operating system, socket shields the network details of the TCP/IP network transport layer and below, and provides network abstraction for the operating system. Network programs written by developers will use socket abstraction directly or indirectly. Through the socket abstraction, the two major protocols (TCP, UDP) of the transport layer can be directly controlled, and even network layer protocols such as IP and ICMP can be controlled.
Simply put, a socket is a triplet of IP address + port + protocol that uniquely identifies a communication link, and a communication link on the server side can correspond to multiple clients.
Fourth, the type of socket
1. Filesystem-based socket family
Socket family name: AF_UNIX
As mentioned earlier, socket was originally used as a process mechanism for UNIX. Everything in UNIX is a file. It is a file-based socket that calls the underlying file system to fetch data. Two socket processes run on the same machine. Communication is done indirectly by accessing the same file system.
2. Socket family based on network type:
Network type socket name: AF_INET
IPv4 based sockets, additionally AF_INET6 for IPv6
Five, socket programming
1. Socket workflow
2. Simple implementation of socket based on TCP
#Server side import socket #Instantiate a socket object and specify the network layer protocol, transport layer protocol socket_server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Bind IP , port number socket_server.bind(( ' 127.0.0.1 ' ,8080 ) ,) #Requests that can be received at the same time socket_server.listen(5 ) #Waiting for connection connect,client_address = socket_server.accept() #Set the received message, the maximum limit is 1024 receive_mag = connect.recv(1024 ) print ( ' Received Client's message: %s ' % receive_mag.decode( ' UTF-8 ' )) #send data connect.send(receive_mag.upper()) #Close the connection connect.close() #client import socket _ socket_client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Connect to the server, specify the IP and port number socket_client.connect(( ' 127.0.0.1 ' ,8080 )) # Send a message to the server socket_client.send( ' Hello ,I am client! ' .encode( ' utf-8 ' )) data = socket_client.recv(1024 ) print ( ' Received message from server: %s ' % data.decode( ' utf-8 ' )) socket_client.close()
3. TCP-based socket connection loop and communication loop
#Server side import socket phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) phone.bind(('127.0.0.1',8080)) phone.listen( 5 ) while True: #Connection loop connect,client_address = phone.accept() print ( ' Client: ' ,client_address) while True: #Communication loop try : msg = connect.recv(1024 ) print ( ' Client message: ' ,msg) connect.send(msg+b'from server') except ConnectionResetError: break connect.close() phone.close() #client import socket _ phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) phone.connect(( ' 127.0.0.1 ' ,8080)) #Dial the phone, the address is the server's ip and port while True: msg = input( ' >>>).strip() phone.send(msg.encode( ' utf-8 ' )) #Send a message data = phone.recv(1024) #Receive a message print (data.decode( ' utf -8 ' )) phone.close()