Setting up a UDP Socket

We're now going to write some basic server code in UDP. Let's get right into it.

Remember that sockets are just software endpoints that processes write and read data from. They are bound to an IP address and a port. As we will see, the sending process attaches the IP address and port number of the receiving application. The IP address and port number of the sending process are also attached to the packets as headers, but that’s not done manually in the code of the application itself. Networking libraries are provided with nearly all programming languages and they take responsibility for lots of plumbing.

Purpose of the Program

Let’s set up a socket for a UDP server program that works like so:

  1. The client will send a line of text to the server.
  2. The server will receive the data and convert each character to uppercase.
  3. The server will send the uppercase characters to the client.
  4. The client will receive and display them on its screen.

We are building the code line-by-line to make it easier to understand. Moreover, we will highlight new additions at every step.

Importing socket

The first step when writing a network application in Python is to import the socket library. It’s generally already part of the Python bundle, so no extra library will have to be manually installed.

Get hands-on with 1200+ tech skills courses.