TCP and Concurrent Server
Unlike UDP, it is a connection-oriented, reliable data transmission protocol.
TCP Communication is Complicated
Write a TCP server first. The general steps are as follows:
1. The first step is to create a socket socket: socket()
2. Servers usually need a fixed IP address and a fixed port number. Servers need to bind this IP address and port number: bind()
3. There is a certain number of clients connecting to the server (the maximum number of connections allowed), and this number is set by the server: listen()
4. The above three steps are to prepare for being connected. This step is to receive data from the client. Before receiving data, the server still needs to accept the socket socket and IP address of the client: accept().
5. Only when the server and the client bind the same IP address, can the server and the client receive and send data to each other. Generally speaking, the server is open and receives data directly. So in the process of receiving data, we need an infinite loop: recv().
6. If we want to send data to the client, the client also needs to bind the address above, and then we need to call the corresponding function send().
import socket # TCP # Create a server socket socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Server IP Address and port number address = ("127.0.0.1", 9999) # Server Binding Address server_socket.bind(address) # Set the maximum number of connections to the server server_socket.listen(1) # Create a client waiting to connect socket Sockets, accept()The function returns two values, which can be printed on its own. client_socket, address = server_socket.accept() # Servers need to constantly receive data from clients while True: """ //In case the client connection fails, anomalies need to be detected //Whether receiving or sending data, we need to unify data encoding and decoding. """ try: data = client_socket.recv(2048).decode("UTF-8") # data processing print("Data accepted by the server:", data) # Data Accepted by Server: I am a Client client_socket.send("I am a server".encode("UTF-8")) except: break
Write another TCP client, the general steps are as follows:
1. The first step is to create a socket socket: socket()
2. The client usually does not need a fixed IP address and fixed port number. When connecting to the server, the system will automatically assign the port number.
3. The client must know the IP address and port number of the server before connecting to the server: connect()
4. The first three steps are to prepare the connection. This step is to send the client's data: send().
5. Clients can also always accept data from servers, so in the process of receiving data, we need an infinite loop: recv()
import socket # Client needs to create a socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Clients need to connect to the server, they must first know the server's IP Address and port number address = ("127.0.0.1", 9999) # Connecting servers client_socket.connect(address) # This data is the data to be sent. data = "I am a client" client_socket.send(data.encode("UTF-8")) while True: """ //In case the server interrupts and exits, anomalies need to be detected //Whether receiving or sending data, we need to unify data encoding and decoding. """ try: data = client_socket.recv(2048).decode("UTF-8") # Data processing, which is printed here and sent back to the client print("Data accepted by the client:", data) except: break
Concurrent servers, here I only introduce multithreaded servers, processes and threads are similar.
Adding a thread to the original TCP server and increasing the number of listeners to 5 allows five client threads to access at the same time and send and receive data at the same time.
import socket from threading import Thread # TCP # Create a server socket socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Server IP Address and port number address = ("127.0.0.1", 9999) def net_connect(): try: # Server Binding Address server_socket.bind(address) # Set the maximum number of connections to the server server_socket.listen(5) except: return # Create threads th = Thread(target=recv_data) th.start() def recv_data(): # Servers need to constantly receive data from clients while True: """ //In case the client connection fails, anomalies need to be detected //Whether receiving or sending data, we need to unify data encoding and decoding. """ # Create a client waiting to connect socket Sockets, accept()The function returns two values, which can be printed on its own. client_socket, address = server_socket.accept()try: data = client_socket.recv(2048).decode("UTF-8") # data processing print("Data accepted by the server:", data) client_socket.send("I am a server".encode("UTF-8")) except: break net_connect()
Adding a thread to the TCP client and running the client program several times at the same time:
import socket # Client needs to create a socket from threading import Thread client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Clients need to connect to the server, they must first know the server's IP Address and port number address = ("127.0.0.1", 9999) def net_connect(): try: # Connecting servers client_socket.connect(address) except: return th = Thread(target=recv_data) # th.setDaemon(True) th.start() def recv_data(): # This data is the data to be sent. data = "I am a client" client_socket.send(data.encode("UTF-8")) while True: """ //In case the server interrupts and exits, anomalies need to be detected //Whether receiving or sending data, we need to unify data encoding and decoding. """ try: data = client_socket.recv(2048).decode("UTF-8") # Data processing, which is printed here and sent back to the client print("Data accepted by the client:", data) except: break net_connect()