23 Apr 18 Socket process based on UDP protocol communication

23 Apr 18
1. Socket communication based on TCP protocol (no concurrent effect)
#blocking: the program stopped
Server:
import socket
 
server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1',8080))
server.listen(5) # listen to receive connection requests from clients
 
while True:
    conn,client_addr=server.accept()
    print(client_addr)
 
    while True:
        try:
            data=conn.recv(1024)
            if not data:break # For linux system
            conn.send(data.upper())
        except ConnectionResetError: #Linux system will not report ConnectionResetError (windows system) when unilaterally closing the connection, it will receive empty
            break
    conn.close()
 
server.close()
Client:
import socket
 
client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))
 
while True:
    msg=input('>>>: ').strip()
    if not msg:continue
    client.send(msg.encode('utf-8'))
    data=client.recv(1024)
    print(data.decode('utf-8'))
 
client.close()
 
2. Sockets based on UDP protocol communication
TCP (Streaming Protocol): High reliability. Packets will be lost due to network reasons, but the information will be deleted in memory only when one end receives the ack confirmation message from the other end. There will be a sticky packet problem in TCP, and receiving and sending may not correspond. The server must be started first, otherwise an error will be reported.
UDP (Packet Protocol): Low reliability. Information is deleted in memory once sent, and if packet loss occurs, the information is lost. The main reasons for the high efficiency and speed of UDP are that one is not establishing a connection, and the other is that it will not be confirmed after acceptance. UDP does not have the problem of sticking packets, and there is a one-to-one correspondence between receiving and sending. If you send hello, but receive a character, an error will be reported in the windows system; in the linunx system, no error will be reported, only h is received.
Parallel: (in the true sense) run concurrently; parallelism is concurrency
Concurrency: It seems to be running at the same time
 
Server:
import socket
 
server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # datagram protocol
server.bind(('127.0.0.1',8080))
 
while True:
client_data,client_addr=server.recvfrom(1024)
#The maximum accepted bytes to keep UDP stable (less packet loss) is 512, so it is generally set to 1024 here
    print(client_data)
    msg=input('回复%s:%s>>>:' %(client_addr[0],client_addr[1]))
server.sendto(msg.encode('utf-8'),client_addr)
 
Client:
import socket
 
client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # datagram protocol
 
while True:
    msg=input('>>>: ').strip()
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
    res,server_addr=client.recvfrom(1024)
    print(res.decode('utf-8'))
#Because UDP does not need to establish a connection in advance, there is no need to wait for one client to exit before another can interact; several clients can interact with the server. The server is still a service, but because of the fast processing speed, it will give users a feeling of concurrency.
 
URL address: http://www.cnblogs.com/linhaifeng/articles/6817679.html
http: #The protocol used by the browser (client socket)
www.cnblogs.com #domain              name
www.cnblogs.com : 80 #Omit: 80, 80 is the port, the default server port is 80
linhaifeng/articles/6817679.html #Path
 
DHCP server: Each computer comes with it, it is used to configure IP, and its port defaults to 67
DNS server: Each computer comes with it, it is used to convert the domain name to IP, its default port is 53
 
Accessing a URL with a browser is essentially to connect to the server, open the file on it and download it locally
a. Browser client socket –– UDP –– DNS server socket, resolve the domain name into IP and return it
b. Browser client socket – – TCP – – Web server socket, download the file to the local
Common server software: nginx, httpd
 
DNS queries are iterative queries
Generally, the local DNS is searched now. If it is not found, it will be searched in the same-level DNS. If it is not found, it needs to be asked from the root DNS server step by step (iterative query); the local DNS will be explained later.
www.cnblogs.com.
. #Talkdomain #13 follower servers (US 9th 1 Euro 3), limited by UDP stability
com #top-level domain
cnblogs #second-level domain name #Second-level domain name needs to go to the top-level domain name to buy
www #hostname of the secondary domain name

3. Process
#serial: run one by one
1. What is a program? What is a process?
    The program is simply a bunch of files
    A process is an executing process/program, so a process is an abstract concept. The concept originates from the operating system
 
2. What is an operating system
    Definition: An operating system is a control program that is located between computer hardware and application software and is used to coordinate, manage, and control the resources of computer hardware and software.
 
3. Two major functions of the operating system:
    1. Encapsulate complex and ugly hardware operations into beautiful interfaces for application use
    2. Make the competition of the process for the hardware in an orderly way
 
4. Multi-channel technology (a single CPU switches in multiple programs to achieve concurrency)
    The background of multi-channel generation is to achieve the effect of concurrent execution of multiple processes in the case of a single cpu
    a. Spatial multiplexing (multiple programs reuse memory space)
    b. Time multiplexing (multiple programs multiplexing CPU time)
    
    CPU encounters IO operations to switch (to improve efficiency)
    A process that occupies the CPU for too long is also cut (reduces efficiency)
The memory space between processes is isolated from each other

Guess you like

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