Sockets and Network Inter-Process Communication

College Depth 68 in the knowledge graph I know this Set as goal
ipc sockets networking

Core Idea

Sockets are the primary mechanism for network communication and can also be used for local IPC via Unix domain sockets. TCP sockets provide reliable, connection-oriented communication; UDP sockets provide connectionless, datagram-based communication. Unix domain sockets enable efficient local inter-process communication without network stack overhead.

Explainer

You have already studied IPC mechanisms like pipes and shared memory, which let processes on the same machine exchange data. Sockets extend this idea across a network — they let processes communicate whether they are on the same machine, across a room, or across the world. A socket is an endpoint for communication, identified by an address and a port number. When two processes each create a socket and connect them, they get a bidirectional communication channel that works through the standard read/write file descriptor interface you already know from Unix I/O.

The two main socket types correspond to two fundamentally different communication models. TCP sockets (SOCK_STREAM) provide a reliable, ordered byte stream — the OS guarantees that data arrives in order, without duplication, and retransmits anything lost in transit. The tradeoff is setup cost: TCP requires a three-way handshake to establish a connection before any data flows. This is the right choice for web servers, databases, SSH sessions, and anything where correctness matters more than latency. UDP sockets (SOCK_DGRAM) provide a connectionless, best-effort datagram service — each send is an independent message with no delivery guarantee. UDP is faster (no handshake, no retransmission overhead) and suits applications like video streaming, DNS lookups, and online games where occasional packet loss is acceptable and low latency is critical.

The typical TCP workflow follows a client-server pattern. The server calls socket() to create a socket, bind() to attach it to an address and port, listen() to mark it as accepting connections, and accept() to wait for a client. The client calls socket() and then connect() to reach the server. After the connection is established, both sides use read() and write() (or send() and recv()) to exchange data, just as they would with a file descriptor. This uniformity — treating network connections like files — is one of Unix's most powerful abstractions.

For processes on the same machine, Unix domain sockets offer the best of both worlds: the socket API's flexibility with the performance of local IPC. Instead of an IP address and port, Unix domain sockets use a filesystem path as their address (e.g., /var/run/app.sock). Data never touches the network stack, so communication is significantly faster than TCP loopback. Many production systems use Unix domain sockets for communication between co-located services — for example, a web server talking to a database on the same host, or a container communicating with its orchestrator. Understanding when to use TCP, UDP, or Unix domain sockets is a practical skill that comes up in nearly every systems programming context.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsOperators and ExpressionsArithmetic Operators and Operator PrecedenceComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsCombinational Circuit DesignFlip-Flops and LatchesBinary Counters: Design and AnalysisBinary ArithmeticFixed-Point Number RepresentationTwo's Complement RepresentationOverflow and Underflow DetectionBinary Adders: Half-Adders and Full-AddersFull Adder and Carry PropagationCarry Lookahead Adder DesignHalf Adder Circuit DesignMultiplication Circuit DesignSequential Circuit DesignRegisters and Register FilesInstruction Set Architecture (ISA)Assembly Language BasicsMemory Organization and AddressingMemory HierarchyCache Memory DesignCache Replacement PoliciesVirtual Memory and PagingVirtual Memory and Demand PagingShared Memory Inter-Process CommunicationSockets and Network Inter-Process Communication

Longest path: 69 steps · 250 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.