Implementing the UDP client

To begin implementing the client, we will need to declare the IP address that we will be trying to send our UDP messages to, as well as the port number. This port number is arbitrary but you must ensure you aren't using a socket that has already been taken:

UDP_IP_ADDRESS = "127.0.0.1" UDP_PORT = 6789 message = "Hello, Server"

Now it's time to create the socket through which we will be sending our UDP message to the server:

clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

And finally, once we've constructed our new socket, it's time to write the code that will send our UDP message:

clientSocket.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT))

You can find the following code in the udp_client.py

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.