Before you can have a multiplayer game you need two programs that can talk to each other reliably. Today we learn the common language of every network: sockets.
In the previous lesson our enemies already "thought" locally, inside a single process. A multiplayer game breaks that assumption: now there are two (or more) programs running, possibly on different computers, that need to share the same world state. The fundamental piece that makes this possible is the socket: a connection endpoint that lets you send and receive data over a network, identified by an IP address and a port.
We'll work with Berkeley sockets (BSD sockets), the POSIX standard used natively by Linux and macOS. On Windows, the API is called WinSock and is nearly identical in usage — the main difference is initialization with `WSAStartup` and needing to link the `ws2_32` library.
TCP guarantees ordered, reliable delivery of data, but has more latency. UDP is faster but doesn't guarantee packets arrive, or in what order. For this lesson and the next we'll use TCP because it's simpler to reason about while you're learning the fundamentals; in a real-time action game with many players, the industry usually prefers UDP with its own reliability layer, but that's outside the scope of an introductory course.
A TCP server always follows the same sequence: create the socket, bind it to an address and port (`bind`), put it into listening mode (`listen`), and accept clients (`accept`).
`htons` converts the port number to network byte order (big-endian), regardless of whether your computer uses little-endian or big-endian internally. This detail — which exists precisely because different CPU architectures represent numbers differently — is a great candidate for asking your AI assistant to explain with a byte diagram if it's not immediately clear.
The client is simpler: it creates the socket and connects directly to a known address and port with `connect`.
You should see "Client connected!" and the exchanged message in both terminals. As small as this seems, it's the complete foundation of every multiplayer game: two independent programs, communicating over a network with a protocol you define.
A very common beginner mistake is assuming a `recv()` call receives exactly what a `send()` sent on the other side. TCP is a continuous byte stream, not a system of discrete messages — it can split a large message across several `recv()` calls, or merge several small messages into one. For a real game protocol you need to define your own "framing" (for example, sending the message size in 4 bytes first, then the content). We'll solve this formally in the next lesson, where we no longer just exchange a greeting message but the continuous state of a real multiplayer game.
In the next lesson we take these exact sockets and build a working multiplayer game, syncing player positions between client and server in real time.
Carlos Montiel is an enterprise AI solutions architect. He implements LLMs, Agents, RAG, and orchestrators for companies across Guatemala and Latin America. Reach out for a consultation.
Contact Carlos Montiel