Skillversitas

Networking

The socket module provides a low-level interface for network communication. Python supports the TCP and UDP protocols.

A socket is the communication endpoint between two programs over a network. TCP (Transmission Control Protocol) guarantees that data arrives in order and intact, suitable for applications such as web browsing and email. UDP (User Datagram Protocol) is faster but does not guarantee delivery, commonly used for video streaming or online games. In the example below, we use SOCK_STREAM, which means TCP.

Socket communication follows a client-server pattern. The server creates a socket, binds it to a specific address and port (bind), then listens for incoming connections (listen). When a client connects, the server accepts the connection (accept) and exchanges data. The client on the other side creates a socket, connects to the server address (connect), then sends or receives data. Understanding this pattern is important because it underlies the HTTP, WebSocket, and gRPC protocols.

Code Example

# Simple TCP server
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 5000))
server.listen(1)
print("Server waiting for connection...")

client, address = server.accept()
print(f"Connected with {address}")

client.send("Hello from the server!".encode())
client.close()
server.close()

# --- Client ---
# client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# client.connect(("localhost", 5000))
# data = client.recv(1024).decode()
# print(data)

Code Explanation

  • socket.socket(socket.AF_INET, socket.SOCK_STREAM) creates a TCP IPv4 socket. AF_INET is for IPv4 addresses, SOCK_STREAM is for the TCP protocol.
  • server.bind(("localhost", 5000)) binds the socket to the local address on port 5000. Ports above 1024 can be used safely without root access.
  • server.listen(1) prepares the server to accept a maximum of 1 connection in the queue.
  • server.accept() blocks execution until a client connects, then returns the client socket object and its address.
  • client.send("Hello from the server!".encode()) sends data in the form of bytes (string encoded to UTF-8).
  • On the client side (commented out), connect connects to the server, recv(1024) receives up to 1024 bytes of data, then decode() converts the bytes back to a string.

Everyday Example

A simple office chat application uses TCP sockets for real-time communication between employees. A central server hosts all connections from each client. When an employee sends a message, the server receives the data through the socket, then forwards it to all other connected clients. The same pattern is used by WhatsApp Web, albeit with a much more complex protocol such as WebSocket and end-to-end encryption.

Summary

Sockets are the foundation of all modern network communication. By mastering the bind-listen-accept pattern on the server and connect-send-recv on the client, you understand how HTTP, database drivers, and other network protocols work. Start with simple TCP, then explore UDP and non-blocking sockets for more complex applications.