In network communication, each connection must create a new thread (or process) to handle it; otherwise, a single thread cannot accept connections from other clients while it is processing the connection. So we try to use concatenation to realize the server's response to multiple clients.
Same construct as single TCP communication, except that concatenation is used to enable multiple tasks to be performed simultaneously.
#server import socket from gevent import monkey import gevent monkey.patch_all() def handle_conn(seObj): while True: re_Data = (1024).decode('utf-8') if re_Data == 'quit': break print('client>>',re_Data) value = input("server>>") se_Data = (('utf-8')) if se_Data == 'quit': break if __name__ == '__main__': server = () (('192.168.1.227',9876)) print("Service is on.") (4) while True: seObj,add = () (handle_conn,seObj) () ()
import socket # 1. create the client socket object client = () # 2. To connect to the server, you need to specify the port and IP address. (('192.168.1.227',9876)) while True: # 3. Send data to the server send_data = input("client:>") (send_data.encode('utf-8')) if send_data == 'quit': break # 4. Getting messages returned by the server recv_data = (1024).decode('utf-8') if recv_data == 'quit': break print("server:>%s" %(recv_data)) # 5. Close the socket connection ()
The above is the details of the code example of implementing TCP connection by concatenation in python, thank you for your support.