When you are connected to a network, the router assigns an IP to your device. This is generally dynamic and you might not get the same IP every time you connect to the network. If you have read my previous post on building a smart music system from scratch then you have understood the need for setting a static IP. It is like reserving a seat on the table.
A little tip, always try to set up a static IP for your devices so that the router doesn’t assign the same IP to other devices preventing an IP conflict.
The process is straight forward.
- Open your settings.
- Go to Wi-Fi.
- Long Press the connected network.
- Select “Modify Network”.
- Check “Advanced Options”.
- On IP address field, set your desired IP.
- Tap Save.
And you are good to go.
Well, your purpose is served but if you are wondering what the terms “Prefix length” or “Default gateway” stand for, then enjoy the next part of the article.
Simply stated, default gateway is the IP address of your router. As the name suggests, through this gateway all the devices on your network communicate to the outer world.
Now to understand prefix length lets study what is an IP address and how it is structured. IP address is simply a unique string of numbers separated by full stops that identifies each device using the Internet Protocol to communicate over a network.
IP can be of 2 types: IPv4 and IPv6. In this article I will be referring to IPv4 as IP address. At a glance an IP looks something like this:
It consists of 4 numbers separated by periods. Each number can hold a value from 0 to 255. Why 255? Each number consists of 8 bits and 255 is the highest decimal value one can obtain with those number of bits (all being high). That is why they are also known as Octets. So an IP address is 4 octets combined together separated by period.
So given an IP address, we can represent it in binary form. Now subnet mask is a binary sequence that determines the number of networks and hosts under the networks. And prefix length is just a shorthand to represent the subnet mask. It is the number of bits set in the subnet mask. Still confused? Let’s take an example:
Consider a subnet mask:
Representing it in binary, we get:
11111111 11111111 11111111 00000000
It is actually a 32 bit number (4 x 8bits), I have displayed it in 4 groups to make it easier to read. So in the subnet there are 24 1’s. Therefore the prefix length of this subnet is 24. Subnet mask is basically a trade-off between the number of networks and the number of hosts under a network. The bits that are set are used to identify the network while the remaining bits identifies the host.
So in step 6, you should be careful while setting the IP address. It must belong to the subnet assigned.
You can also get your subnet mask by typing ifconfig / ipconfig on your terminal / command prompt.
Lastly another thing that you will come across is DNS server. It stands for Domain Name System server. We can access a website by name or by an IP address. DNS server maps IP address to readable domain names (Basic marketing strategy. People tend to remember words than numbers). Generally default gateway is set as the preferred DNS server as it can forward DNS requests to DNS server of your ISP (Internet Service Provider). You can set Google’s DNS server (220.127.116.11 / 18.104.22.168) as your DNS server too.
Although this post was originally meant for android, I think after gaining an understanding, setting static IP on devices in other platforms will be a piece of cake for you.
P.S – For my geeky readers who have tried pinging my website to get an IP address, it will not open my website if you enter the IP in the address bar. The reason is my website is on a shared web hosting. It means that there are multiple websites on the public IP assigned to my server. So, if you are wondering how DNS works in this case, the HTTP header sends a host-name so that it can identify hosts on same server.
Thanks for stopping by. Do let me know for any topic and I will make an article on that.