Sockets and Port Numbers

We need a mechanism which defines multiple end-points within a single host. This is the concept of port numbers and of sockets.

If I need to look up an internet address, I use the Domain Name Systems (DNS). I send a UDP datagram with a random source port number 12345 to the port on the server which accepts requests for the DNS service, port 53. The server responds back to my IP address, destination port 12345, using 53 as its source port.

The same server could be running a Network Time Protocol (NTP) service. I send a UDP datagram with a different random source port number 12346 to the port on the server which accepts requests for the NTP service, port 123. The server responds back to my IP address, destination port 12346, using 123 as its source port.

Port numbers are assigned in various ways, based on three ranges.

  1. System Ports (0-1023) of which 0-256 are called well-known ports

  2. User Ports (1024-49151)

  3. Dynamic and/or Private Ports (49152-65535) The IANA maintains a registry of assigned ports.

In Python, if I want to send data to a server, I need both the IPv4 address and the port number.

s.sendto(message, (UDP_IP, UDP_PORT))

If I'm running a server, I bind the application I am running to the port.

s.bind( (UDP_IP, UDP_PORT) )

No other programme can bind to this port until the programme terminates or I release it.

Last updated