Functional programming: python3 uses socket module to simulate TCP communication-multi-process multi-client connection

To simulate the TCP communication process, this example connects multiple times, using a multi-process method. The function realized is that the multi-client connects to the server port 21567, the connection method is TCP, send data to the server, the server prints the data, and then feedback ([current time] send data) to each client

import socket
import os
from time import strftime

host = ''
port = 21567
addr = (host, port)
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(2)

while True: ## The outer loop control server waits to receive the client connection
    cli_sock, cli_addr = s.accept ()
    pid = os.fork () ## Use the fork function of the os module to copy the process.
    if pid: ## Judgment, if pid is not 0, it is equal to the main process
        cli_sock.close () ## The main process is only responsible for the connection of the server, close the connection of the client
        while True: ## Add a loop to control the main process The operation
            result of the zombie process result = os.waitpid (-1, 1) [0] ## Here, -1 represents the C language format, 1 means that the process does not hang, because the result returned by os.waitpid is a yuan Group, the first item of the tuple is the pid of the process. So take the following table 0
            if result == 0: ## Judgment, if the child process pid is 0, it means that it is a zombie process (exit), not 0 is a normal running child process
                break
    else: ## If pid is not 0, it is equal to a child Process
        s.close () ## The child process is responsible for all operations of the client. But not responsible for the operation of the server, so close the server.
        while True: ## The inner loop is responsible for controlling the data reception and transmission of the
            server. rdata = cli_sock.recv (1024)
            rdata = rdata.decode ('utf8')
            if rdata.strip () == 'quit':
                break
            print ( rdata.strip ())
            sdata = '[% s]% s'% (strftime ('% H:% S:% M'), rdata)
            cli_sock.send (sdata.encode ('utf8'))
        cli_sock.close ()
        exit () # #Here is very important. Here is the work that belongs to the child process. If not write exit. Then after the child process completes its work, it will re-enter the loop, and the child process will also generate a child process, resulting in a fork bomb.
s.close ()


This example is multiple connections, functional programming, and program execution is performed from top to bottom. Relatively easy to understand. In general, the server waits to receive data, but when the client connects in, it creates a child process. Judgment, if it is the main process, then close the client connection service. If it is a child process. Close the server connection service. Wait for the client to connect to the main process. Handle other tasks specifically to the child process. Since multiple processes can generate zombie processes, the work of handling zombie processes is handed over to the main process. Finally, note that the child process is inside the outer loop. If the child process does not exit after completing its work. Then when the cycle starts again, a fork bomb will be generated. Continuously spawning child processes.

Published 73 original articles · praised 4 · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_27592485/article/details/100767398