UDP Client and Server Tutorial in Python
This Tutorial will learn to program the UDP Client and Server in Python.
This protocol is using in many applications such as games for connectionless communication without losing any data packets.
Here Client must send the request to the server for any type of action by using the server.
Implementing the Client:
In Python, there is a module called socket which is using for communication with the server.
So, We have to import the socket module as below
After this, we should declare the IP address and the port number.
UDP_IP_ADDRESS="192.168.1.8" UDP_PORT_NUMBER=5678 Message="Hello,Server"
And Ensure that you are not using the socket that has already used.
This time we have to create the socket for sending the messages from the client to the server.
finally, we have to write the code for sending the message to the server.
Implementing the Server:
Like implementing the Client server also has the same code for receiving the messages from the Clients and It is essential
to the server that it should execute its code prior to the Client Python code, Otherwise, it will fail.
And the code for the server is as below
import socket UDP_IP_ADDR="192.168.1.8" UDP_PORT_NUMBER=5678 // We should give the port numbers and the IP address in both Client and the Server as must be same serversocket=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) serversocket.bind((UDP_IP_ADDRESS,UDP_PORT_NUMBER)) while True: data,address=serversocket.recvfrom(1024) print("Message: ",data)
If you want to know about eval() and exec() functions in Python