SoFunction
Updated on 2024-11-16

Example of python UDP (udp) protocol send and receive

Need to create 2 files, one as client and one as server

File 1 as client and file 2 as server.

The feature of udp is that it does not require a connection to be established

Document 1 Client

#No need to establish a connection
import socket
# Create a socket object
#SOCK_DGRAM udp mode
s=(socket.AF_INET,socket.SOCK_DGRAM)
#Send Data Bytes
("Hello.".encode(),("169.254.184.146",8000))

Documentation II server

import socket
# Create a socket object
#SOCK_DGRAM udp mode
s=(socket.AF_INET,socket.SOCK_DGRAM)
(("169.254.184.146",8000)) #Bind the ip and port of the server
data=(1024) # Receive 1024 bytes at a time
print(())# decode()Decode received bytes

Note: Run file two first before running file one.

The above example of this python UDP (udp) protocol send and receive is all that I have shared with you.