Python | udp network program

udp network program


UDP protocol
UDP is the abbreviation of User Datagram Protocol, and the Chinese name is User Datagram Protocol. Before the communication starts, there is no need to establish relevant links, just send data, similar to "writing a letter" in life.

Communication between different computers requires the use of sockets
Socket can communicate between different computers; it can also communicate between different programs on the same computer.

UDP communication model

Udp send data
The process of creating a udp-based network program is very simple. The specific steps are as follows:

Create client socket
send/receive data
close the socket
import socket

# 1. Create socket and connect
# AF_INET: Indicates that this socket is used for network connection
# SOCK_DGRAM: indicates that the connection is a udp connection
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 2. Send data
# data: The data to be sent, it is binary data
# address: who to send to, the parameter is a tuple, there are two elements in the tuple
# The 0th represents the ip address of the target, and the 1st represents the port number of the program

# Send hello to port 9000 of the host 192.168.31.199
# Port number: 0~65536 0~1024 Do not use, some important services of the system are in use
# Find a free port number
s.sendto('Good afternoon'.encode('utf8'), ('192.168.31.199', 9090))

# 3. Close the socket
s.close()
Udp receive data
import socket

# Create a udp-based network socket connection
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind port number and ip address
s.bind(('192.168.31.199', 9090))

# recvfrom receive data
# content = s.recvfrom(1024)
# print(content)
# The received data is a tuple with two elements in the tuple
# The 0th element is the received data, the 1st element is the sender's ip address and port number

data, addr = s.recvfrom(1024) # recvfrom is a blocking method, waiting for
print('Received a message from {}address{}port number, the content is:{}'.format(addr[0], addr[1], data.decode('utf8')))

s.close()

changing port number


Re-run the script several times, and then in the "Network Debugging Assistant", the phenomenon you see is as follows
illustrate:
Every time the network program is re-run, the number in the red circle in the above figure is different because this number identifies the network program. When it is re-run, if it is not determined which one to use, the system will assign it randomly by default.
Remember one thing: this network program uniquely identifies this program when it is running, so if network programs on other computers want to send data to this program, they need to send data to the program identified by this number (ie port). Just send

UDP binding information
<1>. Binding information
Under normal circumstances, there are many network programs running on a computer. In order not to occupy the same port number with other network programs, often in programming, the port number of udp is generally not bound, but if it needs to be made into a server-side If the program needs to be bound, think about it and why? If the alarm call changes every day, the world will be in chaos, so general service programs often require a fixed port number, which is called port binding.

<2>. Binding example

from socket import *

# 1. Create a socket
udp_socket = socket(AF_INET, SOCK_DGRAM)

# 2. Bind local related information, if a network program is not bound, the system will randomly assign
local_addr = ('', 7788) # ip address and port number, ip generally does not need to be written, it means any ip of the machine
udp_socket.bind(local_addr)

# 3. Waiting to receive the data sent by the other party
recv_data = udp_socket.recvfrom(1024) # 1024 indicates the maximum number of bytes received this time

# 4. Display the received data
print(recv_data[0].decode('gbk'))

# 5. Close the socket
udp_socket.close()
operation result:
image.png
<3>.Summary

A udp network program can be unbound. At this time, the operating system will randomly assign a port. If the program is re-run, the port may change.
A udp network program can also bind information (ip address, port number). If the binding is successful, the operating system uses this port number to distinguish whether the received network data belongs to this process.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00