Python Day32: UDP protocol, UDP and TCP protocol differences, as well as its server, client standard codes

# # UDP protocol 

UDP communication flow chart 

# #! [Image-20181206164635501] (https://ws4.sinaimg.cn/large/006tNbRwly1fxx4ditf86j30fp0a6q3u.jpg) 

`` `Python 
udp is no link, to which end will not start error, even though they do not exist and will not address the error, either forced to close without any problems. in addition, due to no connectivity, the server does not need to touch cycle for clients, as long as the receiving cycle, who made the message can be used to handle 

`` ` 

# # difference between TCP and UDP: ` 

`` Python 
TCP: 
    to establish a connection: three-way handshake. 
    Have reliable data transmission, the sender does not receive the reception confirmation message received too, repeated transmission to the given 
    data transmission is byte-stream, congestion control 
    stick package will occur 
    only one communication 
    
UDP: not necessary to establish connection, bind ID / port number is directly transmitted. 
    Unreliable transport, regardless of whether the recipient to receive information. 
    Data transmission is packet-based, no congestion control 
    stick package does not occur, the packet have clear boundaries. 
    Support one to one, one-to-many interactive communication 
TCP applies to the need to ensure data security, integrity arrive too, such as payment, transport and other text

UDP is suitable for, need real-time, high transmission efficiency scenarios, such as video calls, games. 

`` ` 

# # UDP server 

` `` Python 
Import socket 
ip_port = ( ' 127.0.0.1 ' , 8081 ) 
udp_server_sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) # buy mobile phones 
udp_server_sock.bind (ip_port) 

the while True : 
    qq_msg, addr = udp_server_sock.recvfrom (1024 )
     Print ( ' from: a message [% s% s] of: \ 033 [. 1; 44M% S \ 033 [0m ' % (addr [0], addr [. 1] , qq_msg.decode ( ' UTF-. 8 ' ))) 
    back_msg = INPUT ( 'Reply message: ' ) .strip () 
    udp_server_sock.sendto (back_msg.encode ( ' UTF-. 8 ' ), addr) 
`` ` 

# # the UDP client 

` `` Python 
Import Socket 
the BUFSIZE = 1024 
udp_client_socket = socket.socket (Socket .AF_INET, socket.SOCK_DGRAM) 

qq_name_dic = {
     ' dog brother ' :( ' 127.0.0.1 " , 8081 ),
     ' Teletubbies ' :( ' 127.0.0.1 " , 8081 ),
     ' barabara witch ' :(' 127.0.0.1 ' , 8081 ),
     ' Wang Nima ' :( ' 127.0.0.1 ' , 8081 ), 
} 

the while True: 
    qq_name = INPUT ( ' select chat with: ' ) .strip ()
     the while True: 
        MSG = iNPUT ( ' enter message transmission enter: ' ) .strip ()
         IF MSG == ' quit ' : BREAK 
        IF  Not MSG or  Not qq_name or qq_name Not  in qq_name_dic:continue
        udp_client_socket.sendto(msg.encode('utf-8'),qq_name_dic[qq_name])
        back_msg,addr=udp_client_socket.recvfrom(BUFSIZE)
        print('来自[%s:%s]的一条消息:\033[1;44m%s\033[0m' %(addr[0],addr[1],back_msg.decode('utf-8')))
udp_client_socket.close()

```

 

Guess you like

Origin www.cnblogs.com/huhongpeng/p/10960234.html