TCP/IP Basics for Unity Developers

Sharing is caring!

For many, IP is one of the things you can just grasp the basics of and make it through life. However, if you want to build a multiplayer game, more than the basic knowledge of TCP/IP and its protocols is needed. Don’t let the big acronyms scare you — IP is a logically organized standard. Anyone can understand how IP works, given the right explanation. So without further ado, let’s begin.

A Brief Overview of TCP/IP

You most probably recognize the second part of the TCP/IP as Internet Protocol. IP is the collection of protocols that addresses and routes packets between hosts.

TCP stands for Transmission Control Protocol. It helps send and deliver IP packets between hosts, as well as provide error recovery. Confused? This example should help clear things up.

Think of TCP as the post office, and IP as the street address that a house has. Now, you can put your message into an envelope and write your address on it. However, you can’t expect the letter to send itself just by sitting it on your desk. Of course, you put the letter in your mailbox and the post office picks it up. Most of the time you can be sure that the letter has arrived at the destination, because that is the job of the post office.

The relationship between IP and TCP is very much like that of the aforementioned example. IP puts a destination and source IP address into a packet, like the letter. TCP makes sure that the letter has arrived at it’s destination, just like the post office. Together, they make sure that the data gets to where it needs to go.

TCP Alternative

Just like there isn’t only one package carrier, TCP isn’t the only transmission protocol out there. UDP or User Datagram Protocol is like TCP’s more simple cousin. Unlike TCP, UDP does not guarantee that all the data will get to it’s destination, or in the right order. However, UDP is superior in simplicity due to it’s low overhead. This makes it a popular protocol for applications that do not need guaranteed data delivery. However, TCP is the most popular transmission protocol for games (simply because you want to make sure that things like the users input get sent to the server).

IP Addressing

The IP ‘suite’ as it is called, also contains the rules all IP devices should follow for having addresses. You most definitely interact with IP addressing every day. Whether you type a website into a web browser, or SSH into a remote server, you are using IP addressing.

IP addresses, specificall IPv4 addresses will always have four octets (meaning four 8-bit numbers). For example in the IP address 1.2.3.4, the 1 is the first octet, the 2 is the second and so on. This denotation is called Dotted Decimal Notation, or DDN for short. If you do the math, 4 x 8 bits= 32 bits so an IP Address written in DDN is really just a decimal notation of a 32 bit number.

Subnet Masks

When looking at the IP configuration of your computer (the command in windows is ipconfig), you may have noticed a slash on the end of an IP address, followed by one or two numbers e.x. 192.168.1.10/24. This slash followed by a number is called a subnet mask. Remember how an IP address is just a 32 bit number? Well a subnet mask simply tells how many bits can not be changed in an address to assign to different hosts.

Imagine the address 1.2.3.4 again, but this time it has a subnet mask of /24. This means that 24 bits in the address are static — that is they have to stay the same for all hosts in the same network. With some simple math that tells us that the first three parts of the IP address cannot be changed (8 bits per octet or ‘part’).

Again with some more simple math we can see that with a subnet mask of /24, only 8 bits can be changed, or one octect. So with an IP address of 1.2.3.4/24, 1.2.3 are static and the last part can be any 8-bit number (0-255). Note that the first and last addresses in a subnet are reserved, meaning you can’t assign them to a host.

More on TCP

For the scope of networking with Unity, there are only a few more things you need to know about TCP.

  1. TCP guarantees data will get from point A to point B.
  2. Before data is sent, the TCP ‘app’ running on both hosts with setup a connection.
  3. The amount of data sent by TCP changes based on network conditions, and host abilities.
  4. TCP connects between computers using ports

Look at point four again. You probably have had to open a port on your router to allow a specific application through. Or you may know that HTTP operates on port 80, or SSH port 22. Ports allow hosts to have more than one conversation at the same time by labeling traffic with a port number, specific to an application.

You can see here that TCP is a reliable protocol, and you can count on (most of the time) the data reaching its destination. TCP also makes sure that neither hosts get overwhelmed with data by dynamically changing how much it sends and how fast.

Using TCP/IP With Unity

Before we conclude, lets take some time and implement what we learned with some code. For now, we are just going to initiate a TCP connection between a client and server.

First, the ‘client’ code.

 

Please note that you will need to replace the IP address to the one your machine is currently running. As you can see from the comments we define an IP Endpoint with an IP address and port number. We then setup a socket (basically a virtual ‘socket’ that hosts can ‘plug’ into), using TCP as our transmission protocol.

After that we try to connect to the remote host and if it succeeds, it will log a message. As you can tell it terminates the connection after five seconds. Also note that this code is not optimized at all, it is just used as a starting point to get familiar with low-level networking in C# and Unity.

Now the ‘server’ code.

 

We start similar to the client — by setting up a IP address and port number to use. Then we bind the socket to our IP and wait for a connection. When a connection is successful, we create a ‘handler’ socket that holds the client socket’s information.

Because in this example we know the client is going to disconnect no matter what, we simply do nothing until the connection is ended.

Where To Go From Here

Hopefully you came out of this learning something new about TCP/IP networking.

If you want to get started on high-level networking in Unity right away, read our post on Top 5 Networking Tools for Unity. All of the assets mentioned there are easy to use and need no low-level network coding to get working.

If you want to learn more about sockets, perhaps even send data, check out the Microsoft C# code examples for a Server and a Client. You can also find asynchronous client/server examples in the nav bar.

If you don’t want to write low-level networking code, but instead want high-level control over data, you can use Unity’s built in Networking Engine instead.

If you liked this tutorial, make sure to follow us on twitter to get notified when we post new content (like this)!