Based sockets (link loop) communications protocol tcp

Achieve results: a plurality of clients can only be executed, only to wait a closed, next to then communicate

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()
Client
# 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 ()
Server

 

Guess you like

Origin www.cnblogs.com/zhouhao123/p/11267552.html