UDP Protocol Basics
At the IP network level, all packets are transmitted to a specified host
Source IP-Destination IP
But there may be many separate applications between the two machines that need to communicate, so in order to differentiate between different applications, there is a port number
Source (ip:port number), Destination (ip:port number)
With these four variables, you can determine a specific session.
When the client communicates with the server, the server is assigned a fixed port number. For example, DNS server port 53
And the client will have a randomly selected port number such as Pport 4137
The UDP protocol passes the packet directly from source to Destination
But
How does the client know what the server's port number is?
1. The Convention:iana organization has assigned a fixed port number for many well-known services, such as Port 53 being a DNS
2. Automatic configuration: When a computer first accesses the network, the DHCP protocol can be used to obtain the IP address of the service, which can be accessed by combining the acquired IP address with the port number of the common service .
3. Manual configuration: In the remainder of the situation, manual distribution can be used. Each time you get a service, enter the IP address and port number.
Iaan Port number Assignment
The port number of the most common and widely used service for the well known ports (0-1023) . In UNIX systems, these port numbers cannot be used by user programs to prevent malicious programs from masquerading as important services.
registered Ports (1024-19151) These ports, which are typically not considered special by the operating system, can be used by user-written programs. However, these ports can register special services with the IANA. The IANA recommendation is best to avoid assigning ports to unrelated programs.
The remaining port numbers (49152–65535) These ports are free to use, and the client randomly selects the port number that is usually chosen from here.
Each port number has a non-numeric name. In Python, you can use Getserverbyname to get the port numbers for these non-numeric names.
Import socketsocket.getservbyname ('domain')
For example, the code above can get the port number "53" of the domain service
The famous service name and its port number are usually stored in the/etc/services directory of Linux and Mac.
Sockets
ImportArgparse, Socket fromDatetimeImportdatetimemax_bytes= 65535defServer (port): sock=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Sock.bind (('127.0.0.1', Port)) Print('Listening at {}'. Format (Sock.getsockname ())) whileTrue:data, Address=Sock.recvfrom (max_bytes) Text= Data.decode ('ASCII')Print('The client at {} says {!r}'. Format (address, text)) text='Your data was {} bytes long'. Format (len data) data= Text.encode ('ASCII') Sock.sendto (data, address)defClient (port): sock=Socket.socket (socket.af_inet, socket. SOCK_DGRAM) Text='The time is {}'. Format (DateTime.Now ()) Data= Text.encode ('ASCII') sock.sendto (data, ('127.0.0.1', Port)) Print('The OS assigned me the address {}'. Format (Sock.getsockname ())) data, address= Sock.recvfrom (max_bytes)#danger!Text = Data.decode ('ASCII')Print('The server {} replied {!r}'. Format (address, text))
Using the socket () method in the above code creates a socket, af_inet is the socket type, and Sock_dgrame is the packet data type, which means that it will use UDP in the IP network.
The socket is then bound together by the Blind Method and A (Ip,port). (If the port number is already in use, this step will fail.) OSError: [Errno 98] Address already in use)
Socket.getsockname () returns a tuple that contains the IP address and port number.
Socket.recvfrom (max_bytes) tells the program server that it will accept messages with a maximum length of 65535. Recvfrom will wait until a data is successfully received from the client.
Once a message is received, Recvform () returns the client address and the contents of the packet it sends. Use Python to convert these packets into strings and output them.
if __name__=='__main__': Choices= {'Client': Client,'Server': Server} parser= Argparse. Argumentparser (description='Send and receive UDP locally') parser.add_argument ('role', Choices=choices, help='which role to play') parser.add_argument ('- P', metavar='PORT', Type=int, default=1060,help='UDP Port (default 1060)') args=Parser.parse_args () function=Choices[args.role] Function (ARGS.P)
This code, through the command line parameters choose to execute the client or the server, the program. -P to set the port number
Promiscuous CLIENT
A client may receive and record all packets it receives, and it does not consider whether the packet is from the correct address, as such clients call the promiscuous client.
Such a client can be used to monitor the network, but he may also have problems causing the client to receive spurious packets. In order to avoid this problem need to do a highlight check
Python Network Programming reading notes X UDP