1.TCP enables the server to communicate with multiple clients

import socket

def main() :
    # 1. Create a socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2. Bind local information
    tcp_server_socket.bind(("".7890))

    # 3. Change the default socket from active to passive LISTEN
    tcp_server_socket.listen(128)

    while True:
        print("Waiting for a new client to arrive...")
        # 4. Waiting for someone else's call (waiting for the client's link to accept)
        new_client_socket, client_addr = tcp_server_socket.accept()

        print("A new client has arrived %s" % str(client_addr))

        Receive requests from clients
        recv_data = new_client_socket.recv(1024) # Note the 1024byte value. The size can be set as required
        print("Request from client :%s" % recv_data.decode("utf-8"))

        Send some data back to the client
        new_client_socket.send("hahahghai-----ok-----".encode("utf-8"))

        Close the socket
        # Closing the socket returned by Accept means that the client is not being served
        new_client_socket.close()
        print("Server complete...")

    If the listening socket is closed, it will not be able to wait for a new client to arrive, i.e. xxxx.accept will fail
    tcp_server_socket.close()


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

2.TCP enables multiple communication with multiple clients

import socket

def main() :
    # 1. Create a socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2. Bind local information
    tcp_server_socket.bind(("".7890))

    # 3. Change the default socket from active to passive LISTEN
    tcp_server_socket.listen(128)

    # Loop purpose: Call Accept multiple times to serve multiple clients
    while True:
        print("Waiting for a new client to arrive...")
        # 4. Waiting for someone else's call (waiting for the client's link to accept)
        new_client_socket, client_addr = tcp_server_socket.accept()

        print("A new client has arrived %s" % str(client_addr))
        
        # Loop purpose: Serve the same client multiple times
        while True:
            Receive requests from clients
            recv_data = new_client_socket.recv(1024)
            print("Request from client :%s" % recv_data.decode("utf-8"))

            If recV unjams, there are two ways:
            # 1. The client sends data
            # 2. Recv is unplugged when the client calls close.
            if recv_data:
                Send some data back to the client
                new_client_socket.send("hahahghai-----ok-----".encode("utf-8"))
            else:
                break

        Close the socket
        # Closing the socket returned by Accept means that the client is not being served
        new_client_socket.close()
        print("Finished serving this client...")

    If the listening socket is closed, it will not be able to wait for a new client to arrive, i.e. xxxx.accept will fail
    tcp_server_socket.close()


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

You can use the network debugging assistant to realize multiple clients to communicate with it at the same time, but the problem must be one by one communication, queuing.

Unified statement: About the original blog content, there may be some content reference from the Internet, if there is an original link will be quoted; If can not find the original link, in this statement if there is infringement please contact to delete ha. About reprint blog, if have original link will declare; If can not find the original link, in this statement if there is infringement please contact to delete ha.