From the free course Systems Programming in C and Unix Environments on Lykke

Network Programming and Sockets

Key concepts: Layered Network Model · IP Addressing (IPv4/IPv6) · Socket API · TCP vs. UDP · Network Address Translation (NAT)

The layered network model and implementing communication between processes over a network.

Network Programming and Sockets

Network programming represents the logical evolution of Inter-Process Communication (IPC). While tools like pipes, shared memory, and signals allow processes on a single host to coordinate, network programming extends this capability across the global infrastructure of the Internet. At its core, network programming is the art of managing data exchange between two or more autonomous systems, often with vastly different architectures, through a standardized set of protocols and interfaces.

The Layered Network Model

To manage the immense complexity of global communication, networking is organized into a hierarchy of layers. Each layer provides a specific service to the layer above it while hiding the implementation details of the layers below. This abstraction is known as the Network Stack.

The OSI vs. TCP/IP Models

The theoretical standard is the OSI (Open Systems Interconnection) Model, which defines seven distinct layers. However, the practical implementation used by the modern Internet is the TCP/IP Model (or Internet Protocol Suite), which collapses these into four or five functional layers.

OSI Layer TCP/IP Layer Functionality Data Unit
Application (7), Presentation (6), Session (5) Application High-level APIs, data formatting (HTTP, FTP, SMTP) Data/Message
Transport (4) Transport End-to-end communication, reliability (TCP, UDP) Segment (TCP) / Datagram (UDP)
Network (3) Internet Routing packets across network boundaries (IP, ICMP) Packet
Data Link (2) Link Physical addressing and media access (Ethernet, Wi-Fi) Frame
Physical (1) Physical Binary transmission over wires, fiber, or radio Bits

Encapsulation and Decapsulation

The fundamental mechanism of the layered model is Encapsulation. When an application sends data:

  1. The Application Layer generates the raw message.
  2. The Transport Layer prepends a header containing port numbers and sequence info.
  3. The Internet Layer prepends a header containing source and destination IP addresses.
  4. The Link Layer prepends a header (MAC addresses) and appends a trailer (checksum).

As the data travels through the network, routers only look at the Internet Layer. When it reaches the destination, the process is reversed (Decapsulation), with each layer stripping its respective header and passing the remaining payload upward.

The End-to-End Principle: A design philosophy in networking which states that features should be implemented in the end hosts rather than the intermediary nodes (routers), unless they can be completely implemented in the network. This is why reliability (TCP) is handled at the Transport layer, not by the routers themselves.

IP Addressing: IPv4 and IPv6

An IP Address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. It serves two principal functions: host or network interface identification and location addressing.

IPv4: The Legacy Standard

IPv4 uses a 32-bit address space, typically expressed in dotted-decimal notation (e.g., 192.168.1.1).

  • Address Space: $2^{32} \approx 4.29$ billion addresses.
  • Structure: Divided into a Network portion and a Host portion, determined by the Subnet Mask.
  • Exhaustion: The limited number of IPv4 addresses led to the development of NAT and the transition to IPv6.

IPv6: The Future Standard

IPv6 uses a 128-bit address space, expressed in hexadecimal (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).

  • Address Space: $2^{128} \approx 3.4 \times 10^{38}$ addresses (roughly 340 undecillion).
  • Simplification: IPv6 removes the need for NAT and simplifies the header structure to improve routing efficiency.
Feature IPv4 IPv6
Address Length 32 bits 128 bits
Notation Dotted Decimal (127.0.0.1) Hexadecimal (::1)
Header Size 20–60 bytes (variable) 40 bytes (fixed)
Fragmentation Done by routers and hosts Done only by the source host
Configuration Manual or DHCP Stateless Address Autoconfiguration (SLAAC)

The Socket API

The Socket is the fundamental abstraction for network I/O in Unix-like systems. Following the POSIX philosophy that "everything is a file," a socket is accessed via a file descriptor.

Socket Identification: The 5-Tuple

A unique network connection is defined by five pieces of information:

  1. Source IP
  2. Source Port
  3. Destination IP
  4. Destination Port
  5. Protocol (TCP or UDP)

The Life Cycle of a Socket

To establish communication, a program must perform a sequence of system calls. The sequence differs between the Server (which waits for connections) and the Client (which initiates them).

Function Role Description
socket() Both Creates a new communication endpoint.
bind() Server Associates the socket with a specific local IP and Port.
listen() Server Puts the socket in a passive state to wait for incoming connections.
connect() Client Initiates a connection to a remote IP and Port.
accept() Server Extracts the first connection request on the queue and creates a new socket for it.
send() / recv() Both Transmits or receives data (for TCP).
close() Both Terminates the connection and releases the file descriptor.

Example: A Minimal TCP Echo Server in C

The following code demonstrates the server-side lifecycle. Note the use of struct sockaddr_in to define the address.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080
#define BACKLOG 5

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    // 1. Create socket (IPv4, TCP)
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    // 2. Bind to Port 8080
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY; // Listen on all interfaces
    address.sin_port = htons(PORT);       // Host-to-Network Short (Endianness)

    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    // 3. Listen
    if (listen(server_fd, BACKLOG) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    printf("Server listening on port %d...\n", PORT);

    // 4. Accept incoming connection
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }

    // 5. Read data
    read(new_socket, buffer, 1024);
    printf("Received: %s\n", buffer);
    
    // 6. Echo back
    send(new_socket, buffer, strlen(buffer), 0);
    
    close(new_socket);
    close(server_fd);
    return 0;
}

Transport Layer: TCP vs. UDP

The Transport Layer offers two primary protocols that provide different guarantees. Choosing between them is a trade-off between reliability and latency.

TCP (Transmission Control Protocol)

TCP is a connection-oriented protocol that provides a reliable, ordered, and error-checked stream of octets.

  • The Three-Way Handshake: Before data flows, the client and server exchange SYN, SYN-ACK, and ACK packets.
  • Flow Control: Uses a "sliding window" to ensure the sender doesn't overwhelm the receiver.
  • Congestion Control: Detects network bottlenecks and slows down transmission.
  • Retransmission: If a packet is lost (no ACK received), TCP automatically resends it.

UDP (User Datagram Protocol)

UDP is a connectionless, "best-effort" protocol. It sends packets (datagrams) without establishing a connection or verifying receipt.

  • Speed: No handshake and no overhead for ordering or reliability.
  • Use Cases: Real-time applications like VoIP, online gaming, and DNS where a dropped packet is better than a delayed one.
Feature TCP UDP
Connection Connection-oriented Connectionless
Reliability Guaranteed delivery Best effort (no guarantee)
Ordering Strict ordering No ordering
Speed Slower (due to overhead) Faster
Data Unit Segment (Stream) Datagram (Message)
Examples HTTP, SSH, FTP DNS, DHCP, Streaming

Network Address Translation (NAT)

NAT is a method of remapping one IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device.

Motivation: The IPv4 Crunch

Because IPv4 only supports ~4 billion addresses, we cannot give every smartphone, laptop, and IoT toaster a unique public IP. NAT allows a whole private network (e.g., your home Wi-Fi) to share a single public IP address provided by the ISP.

How NAT Works

  1. Private Outbound: A host with private IP 192.168.1.10 sends a packet to a web server.
  2. Translation: The router intercepts the packet, changes the source IP to its Public IP (e.g., 203.0.113.5), and assigns a unique Source Port.
  3. Mapping Table: The router stores this mapping (Private IP + Port $\leftrightarrow$ Public Port) in a NAT table.
  4. Return Path: When the web server replies, the router looks at the destination port, finds the corresponding private IP in the table, and forwards the packet.

Common Pitfalls: "The NAT Hole"

NAT makes it difficult for external hosts to initiate a connection to an internal host (since there is no entry in the mapping table). This requires techniques like Port Forwarding or STUN/TURN protocols for Peer-to-Peer (P2P) applications.

Advanced Topics and Common Pitfalls

Endianness (Byte Order)

Different CPU architectures store multi-byte integers differently.

  • Big-Endian: Most significant byte first.
  • Little-Endian: Least significant byte first (common in x86).
  • Network Byte Order: The Internet standard is Big-Endian.

When programming sockets, you must use functions like htons() (host-to-network-short) and ntohl() (network-to-host-long) to ensure the port numbers and IP addresses are interpreted correctly by the network hardware.

Blocking vs. Non-blocking I/O

By default, recv() and accept() are blocking calls; the process sleeps until data arrives. In high-performance servers, engineers use Non-blocking I/O or I/O Multiplexing (select, poll, or epoll) to handle thousands of concurrent connections in a single thread.

The "Zombies" of Networking: TIME_WAIT

When a TCP connection is closed, the socket enters a TIME_WAIT state for a few minutes. This prevents "delayed" packets from a previous connection from being incorrectly accepted by a new connection using the same port. A common error for beginners is Address already in use when restarting a server; this can be bypassed using the SO_REUSEADDR socket option.

Network Programming and Sockets - Systems Programming in C and Unix Environments - image 1
Network Programming and Sockets - Systems Programming in C and Unix Environments - image 1
Network Programming and Sockets - Systems Programming in C and Unix Environments - diagram 1
Network Programming and Sockets - Systems Programming in C and Unix Environments - diagram 1
Network Programming and Sockets - Systems Programming in C and Unix Environments - diagram 2
Network Programming and Sockets - Systems Programming in C and Unix Environments - diagram 2

Source Materials

Study Systems Programming in C and Unix Environments on Lykke

This course page is free to open: signed out, you can read its first two sections, and signing in, on any plan, opens the rest. The free Lykke Chrome extension syncs your own Canvas courses, so ChatGPT, Claude and Muse can answer from your classes and deadlines. It only reads Canvas.

Add the free extensionSign in to open every section

All sections of Systems Programming in C and Unix Environments · Browse more course pages