A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Socket Programming and Network APIs

Graduate Depth 93 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
363prerequisites beneath it
See this on the map →
Port Addressing and SocketsSystem Calls and User/Kernel Mode→→Packet Analysis and Network Troubleshooting Tools
socket-api programming tcp-client udp-client bind-listen-accept

Core Idea

Sockets are the primary API for network programming. TCP servers use socket(), bind(), listen(), and accept() to receive connections; clients use socket() and connect(). UDP uses sendto() and recvfrom(). Understanding socket semantics is essential for building networked applications.

Explainer

You already know that port numbers identify specific processes on a host and that system calls are how user programs request services from the operating system kernel. A socket is the meeting point of these two ideas: it is an OS-managed endpoint for network communication, created and manipulated through a small set of system calls. When you create a socket, the kernel allocates internal buffers and state to track a network connection, and gives your program a file descriptor — an integer handle you use for all subsequent operations, just like reading or writing a file.

The TCP server workflow follows a predictable sequence. First, `socket()` creates an endpoint and specifies the protocol (TCP or UDP). Then `bind()` attaches it to a specific IP address and port number — this is where your knowledge of port addressing comes in, because bind tells the OS "I want to receive traffic arriving on port 8080." Next, `listen()` marks the socket as passive, meaning it will accept incoming connections rather than initiate them, and sets a backlog queue size for pending connections. Finally, `accept()` blocks until a client connects, then returns a *new* socket dedicated to that specific client conversation. The original listening socket stays open to accept more clients. This distinction — the listening socket versus the connected socket — is one of the most important concepts in socket programming.

The client side is simpler: `socket()` to create the endpoint, then `connect()` to initiate a TCP handshake with the server's IP and port. Once connected, both sides use `send()` and `recv()` (or `read()` and `write()`) to exchange data over the established connection. UDP skips the connection setup entirely — since it is connectionless, you use `sendto()` and `recvfrom()`, specifying the destination address with every message. There is no handshake, no connection state, and no guaranteed delivery, which matches UDP's lightweight design.

A common stumbling point is that sockets are blocking by default: `accept()` waits until a client connects, `recv()` waits until data arrives. For a server handling multiple clients, this means you need a concurrency strategy — spawning threads or processes per connection, or using non-blocking I/O with `select()` or `poll()` to monitor multiple sockets simultaneously. The socket API is deliberately low-level, giving you direct control over how your program interacts with the transport layer. Higher-level abstractions like HTTP libraries are built on top of sockets, but understanding the raw API lets you reason about what those abstractions are actually doing underneath.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Socket Programming and Network APIs

Longest path: 94 steps · 363 total prerequisite topics

Prerequisites (2)

Leads To (1)