Unity Multiplayer Development
Networking Fundamentals
The Client-Server Model
Most multiplayer games rely on a client-server architecture. It's a simple idea: one computer acts as the central authority (the server), and all other players connect to it (the clients).
Think of it like a library. The library is the server, holding all the books (the game's data). You and other readers are the clients. You request a book from the librarian, who finds it and delivers it to you. You don't grab books directly from other readers; you go through the central authority. This keeps everything organized.
In a game, your computer (the client) tells the server, "I moved forward!" or "I fired my weapon!" The server processes these actions from all players. It then updates the official game state and tells every client, "Player A is now at these coordinates. Player B's health is now 50." This prevents cheating, as the server has the final say on what's happening in the game world.
Protocols TCP vs UDP
For clients and servers to communicate, they need to agree on a set of rules, called a protocol. The two most common protocols on the internet are TCP and UDP. Choosing the right one is critical for a multiplayer game.
Transmission Control Protocol (TCP) is like sending a certified letter. It guarantees that all your data arrives, in the correct order. The recipient has to sign for it, confirming they got it. If a piece of data gets lost, TCP automatically resends it. This reliability is great for things like web browsing or sending an email, where every single piece of information must be perfect.
But that guarantee comes at a cost: speed. All that checking and resending takes time, adding latency. In a fast-paced action game, waiting for a lost packet of data from three seconds ago is useless. The game has already moved on.
User Datagram Protocol (UDP) is like sending a postcard. You write a message, put it in the mail, and hope for the best. It's incredibly fast because there's no error checking or re-sending. If the postcard gets lost, it's gone for good. There's also no guarantee that postcards will arrive in the order you sent them.
This sounds bad, but for gaming, it's often perfect. For frequent updates like a player's position, you don't care if one packet from a millisecond ago gets lost. You're about to get a brand new one with more current information anyway. Speed is more important than perfect reliability for most real-time game data.
| Feature | TCP (Transmission Control Protocol) | UDP (User Datagram Protocol) |
|---|---|---|
| Reliability | High - Guaranteed delivery | Low - No delivery guarantee |
| Ordering | In-order delivery | Unordered delivery |
| Speed | Slower, due to overhead | Faster, less overhead |
| Connection | Connection-based (handshake required) | Connectionless (fire and forget) |
| Best For | Web, email, file transfers | Streaming, online games, VoIP |
Sockets The Doorways
So how does data actually get from one computer to another? Through sockets.
A socket is an endpoint for communication. Think of a computer's IP address as its street address. A socket's port number is like a specific apartment number or office door at that address. When you want to send data to a specific application (like a game server), you send it to that computer's IP address and the specific port number the game is listening on.
For example, your game client might open a socket to connect to the game server at 203.0.113.10 on port 7777. This combination of IP address and port number creates a unique communication channel between your client and the server.
Now that you understand these core concepts, let's review.
Ready to test your knowledge?
In a client-server game architecture, what is the server's primary responsibility?
For sending frequent player position updates in a fast-paced action game, why is UDP generally preferred over TCP?
Understanding these networking building blocks is the first step to creating any multiplayer experience. They are the foundation upon which all online interactions are built.