Computer Network Notes: Implementing Simple UDP and TCP Communication in Python

Understanding UDP and TCP communication protocols is fundamental to computer network programming. This article explores the basic operational principles of both protocols and demonstrates their implementation through practical Python examples.

UDP and TCP Basic Operation Processes

UDP (User Datagram Protocol):

UDP follows a straightforward communication model where the client creates a socket and the server creates its own socket. The client constructs a datagram containing the server's IP address and port, which is then transmitted through the cilent's socket. The server reads the UDP segment from its socket, processes it, and sends a response back to the client's address and port through its socket. Finally, the client receives the response through its socket, and both parties close their respective sockets.

The key characteristic of UDP is its connectionless nature. Each datagram is independent, and the server cannot distinguish between different clients based on socket connections alone.

TCP (Transmission Control Protocol):

TCP operates differently as a connection-oriented protocol. The server creates a listening socket and waits for incoming connection requests. When a client initiates a connection, the server accepts it and creates a dedicated socket for that specific client connection.

The connection establishment involves a three-way handshake between the client's socket and the server's welcoming socket. Once connected, a dedicated channel exists between the client and the server's connection socket. The client sends requests through its socket, the server reads and processes thece requests, and returns responses. After data transfer completes, both parties close their sockets, and the server returns to waiting for the next connection request.

The primary distinction lies in TCP's reliability. Since a dedicated channel is established before data transfer, packets are delivered in order, and the protocol guarantees reliable delivery. UDP, lacking this connection mechanism, cannot guarantee order or delivery.

Implementing a Simple Case Conversion Service

The following examples demonstrate a simple uppercase conversion service where clients send lowercase text and receive uppercase responses.

UDP Implementation

Client-side code:

from socket import *

server_name = 'localhost'
server_port = 12000

# Create UDP socket (AF_INET for IPv4, SOCK_DGRAM for UDP)
client_socket = socket(AF_INET, SOCK_DGRAM)

user_input = input('Enter lowercase text: ')

# Send encoded message to server
client_socket.sendto(user_input.encode(), (server_name, server_port))

# Receive response from server
response_data, server_address = client_socket.recvfrom(2048)

print('Response from server:', response_data.decode())
client_socket.close()

Server-side code:

from socket import *

server_port = 12000

# Create UDP socket and bind to port
server_socket = socket(AF_INET, SOCK_DGRAM)
server_socket.bind(('', server_port))

print('UDP server is ready and listening...')

while True:
    # Receive data from client
    message, client_address = server_socket.recvfrom(2048)
    
    # Process: convert to uppercase
    modified_message = message.decode().upper()
    
    # Send response back to client
    server_socket.sendto(modified_message.encode(), client_address)

TCP Implementation

Cliant-side code:

from socket import *

server_name = 'localhost'
server_port = 12000

# Create TCP socket (SOCK_STREAM indicates TCP)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect((server_name, server_port))

user_input = input('Enter lowercase text: ')

# Send encoded data through established connection
client_socket.send(user_input.encode())

# Receive response
response_data = client_socket.recv(2048)
print('Response from server:', response_data.decode())

client_socket.close()

Server-side code:

from socket import *

server_port = 12000

# Create TCP listening socket
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind(('', server_port))

# Allow the server to accept connections (parameter is backlog)
server_socket.listen(1)

print('TCP server is ready and listening...')

while True:
    # Accept incoming connection
    connection_socket, client_address = server_socket.accept()
    
    # Receive data from client
    received_data = connection_socket.recv(1024).decode()
    
    # Process: convert to uppercase
    result = received_data.upper()
    
    # Send response
    connection_socket.send(result.encode())
    
    # Close connection socket (server socket remains open)
    connection_socket.close()

The UDP implementation uses sendto() and recvfrom() methods, which include address information with each transmission. The TCP implementation establishes a connection first using connect(), then transfers data through send() and recv() without needing to specify addresses for each message. The server uses listen() to monitor incoming connections and accept() to create dedicated sockets for each client connection.

Tags: python socket udp tcp networking

Posted on Sat, 30 May 2026 23:36:59 +0000 by skylert