Socket communication principles underlying
the application memory to copy the operating system, the operating system to follow the TCP protocol to send the other party, the other party receives the transmission signal and
from socket import * client = socket(AF_INET, SOCK_STREAM) client.connect(('127.0.0.1', 8081)) # 通信循环 while True: msg=input('>>: ').strip() #msg='' if len(msg) == 0:continue client.send(msg.encode('utf-8')) #client.send(b'') print('has send') data=client.recv(1024) print('has recv') print(data) client.close()
# Server must meet at least three things: # 1 binds a fixed ip and Port # 2. external services has been stable operation # 3 is capable of supporting concurrent from socket Import * Server = socket (AF_INET, SOCK_STREAM) Server. the bind (( ' 127.0.0.1 ' , 8081 )) server.listen ( . 5 ) # links circulating the while True: Conn, client_addr = server.accept () Print (client_addr) # communications cycle the while True: the try : Data = conn.recv (1024) IF len (Data) == 0: BREAK # for linux system Print ( ' -> receiving the client message: ' , Data) conn.send (data.upper ()) the except ConnectionResetError: BREAK conn.Close () server.close ()