1.UDP agreement
UDP is the User Datagram Protocol short, the Chinese name is the User Datagram Protocol, is the OSI (Open System Interconnection reference model in a non-connected transport layer protocol that provides a simple unreliable messaging service
Is a connectionless transport layer protocol, which is mainly used in order of arrival does not require packet transmission, checks the sort order of packet transmission is completed by the application layer. And does not transmit data packets to ensure the reliability, adapted to transmit a small amount of data.
UDP communication process is very similar to the walkie-talkie
Buy pager == socket () fixed intercom channel == bind () received signal == recvfrom () signaling == sendto () is not necessary to establish the TCP connection will be omitted listen () and accept () both step
3.UDP use
# 客户端 import socket c = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) while True: data = input(">>:") c.sendto(data.encode("utf-8"),("127.0.0.1",9999)) msg = c.recvfrom(1024) print(msg) # 服务器 import socket c = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) c.bind(("127.0.0.1",9999)) while True: msg,addr = c.recvfrom(1024) c.sendto(msg.upper(),addr)
4.UDP specific TCP transmission analysis and comparison:
And emptied immediately after sending UDP data, the data is sent one-time, regardless of success or failure, it will cause data loss, of course, TCP will be lost but there will be automatic retransmission mechanism.
It will not stick package
Further UDP datagram is based, it is sent every time a single data packet, so no problem stick package
test:
# Server accepts only a length greater than the other and receive the transmission data length Import Socket C = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) c.bind (( " 127.0.0.1 " , 9991 )) MSG, addr = C. recvfrom (1024 ) Print (msg.decode ( " UTF-. 8 " )) # client sends data twice Import Socket C = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) c.sendto ( " Hello " .encode ( " UTF-. 8 " ), ( " 127.0.0.1 " , 9991)) c.sendto("world".encode("utf-8"),("127.0.0.1",9991))
Note 1: Data length of the sender should be unified and acceptance of the length of the recipient, or they will lose data, error directly under the windows.
# Modifications in the server embodiment Import Socket C = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) c.bind (( " 127.0.0.1 " , 9991 )) MSG, addr = c.recvfrom (. 1 ) Print (MSG .decode ( " UTF-. 8 " )) # output: # H # W
Note 2: The buffer size can not be infinite, if you want to transfer large data over UDP datagram size, you need to add additional application layer protocol based on UDP.
And even though there will still be enough to buffer data loss, the maximum length of UDP data for 1472
1. In the link layer, the physical characteristics determined by the length of the Ethernet data frame is 64 + 18 + 18-1500, where 18 is the head and tail of the data frame, the data frame means that the content of a maximum of 1500 ( does not include the header and trailer), i.e., MTU (Maximum Transmission unit) 1500; 2. at the network layer, because the IP packet header takes up 20 bytes, so that the MTU of 1500-20 = 1480; 3. transport layer header to the UDP packet to occupy 8 bytes, so that the MTU of 1480-8 = 1472;
Therefore, the application layer, your Data maximum length of 1472. When we UDP packet data is more than MTU (1472), IP layer of the sender needs to transmit fragmented fragmentation at the receiving side of the IP layer datagrams required reorganization, since UDP is an unreliable transport protocol, If fragmentation results in recombination loss failure will cause the UDP packet is discarded.
5. Practice UDP-based time server
Transmitting data packets with a time format to the server
# 服务器 import socket import time c = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) c.bind(("127.0.0.1",8888)) while True: fmt,addr = c.recvfrom(1024) c.sendto(time.strftime(fmt.decode("utf-8"),time.localtime()).encode("utf-8"),addr) # 客户端 import socket c = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) c.sendto("%Y-%m-%d %H:%M:%S %p".encode("utf-8"),("127.0.0.1",8888)) print(c.recvfrom(1024)[0])
7.UDP applications: the DNS
DETAILED process network requests
Our socket in order to complete the program needs to know the IP communications server port, and when we use the browser to access the web page does not specify IP and port of the server, this is why?
We all know that the two computers to communicate, you must know each other's IP address, but IP addresses are hard to remember a more complex, resulting in high cost of Internet user experience is poor, so he thought of ways to take an alias IP address, which is the domain name . As www.baidu.com
domain name:
It is to take an alias IP address, and in order when the query name and an address correspondence faster, it also divided the domain name to a different area.
classification
1.1 Top Level Domain Names, businesses .com .top, network providers .net, non-profit organization .org, .edu education
1.2 international domain names, Chinese .cn, USA .us, .jp Japan
But the attendant problem is that although the domain name simplifies memory, but still have to rely on data transmission IP address and port, so I want also to provide a mechanism ip can be obtained through the domain name, which is DNS
DNS
Qualified domain name resolution server, which is essentially a large database system
DNS access to the process:
When we want to access an address such as www.baidu.com time
The browser will first ask the local DNS server (ie, network operators such as Telecom, China Unicom) to obtain the corresponding IP,
2. If the local DNS does not want to record, then the local DNS, will ask for the root (level 1) Domain Name Server, 13 units worldwide
All the root name servers in the world is impossible to store IP so it only stores the top (level 2) the domain name server IP
For example: IP COM domain master server, NET domain master server IP
3. Thus the local DNS server to obtain IP domain COM send requests,
4. Therefore, also issue a request to the third-level domain corresponding to a domain name host since a plurality of IP
5. Finally, the returned IP information to the local DNS cache spare
DNS issues
Such an action would cause a problem when an existing domain name after changing IP over a period of time is not accessible because the sub-domain name server after a specified time required to reach a record will automatically
8. The current issue
TCP can only handle a client's data
Although UDP processing may appear simultaneously, but essentially are processed in the order of
In order to improve user access speed, it is necessary to find a way so that the server can handle multiple capability of simultaneous client requests
This requires the use of concurrent programming