£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Build a DNS Server in Scratch
Skip main navigation

Build a DNS Server in Scratch

Learn how to create your own DNS server in scratch.

In this step, you’re going to have a go at building your own DNS server in Scratch.

First, go to scratch.mit.edu and create a new project called “DNS model”.

A DNS stores a list of domain names against their IP address. To simulate this, you’re going to create two lists in Scratch: one called “Domain Names” and another called “IP Addresses”.

A screenshot of the web version of Scratch 3. The "Make a List" button in the "Variables" menu is highlighted.

A screenshot of the web version of Scratch 3. On the stage are two empty lists, "Domain Names" and "IP Addresses".

Next, you’ll need to populate each list. About 1000 entries in each should be enough.

Now, this would take you ages to do by hand, but instead, you can download premade lists of Domain Names and IP Addresses from bournetocode.com.

Once you’ve downloaded each file to your computer, right-click the Domain Name box in Scratch and import the domain names into your list. Then do the same thing for IP addresses.

A screenshot of the web version of Scratch 3. On the stage, "Domain Names" has been right-clicked, and the "import" option has been highlighted.

You should end up with something that looks like this.

A screenshot of the web version of Scratch 3. On the stage, the lists "Domain Names" and "IP Addresses" have both been filled with entries. Both lists have a length of 1002.

When you type a URL into the address bar of your web browser, your computer consults the DNS server to find the IP address. The DNS server looks up the domain name and finds the matching IP address. To do this, you can create a linear search algorithm.

Linear search is a method of going through a list like the one we have here, and checking each item one by one until it finds the one it’s looking for.

Start by adding in a “When green flag clicked” block… then ask the user for the domain name they’re looking for.

A screenshot of the web version of Scratch 3. In the scripts area, a “When green flag clicked” block" has been attached to a block reading "ask Enter a URL: and wait".

Next, you’ll need to index the items in each list. This means that when the server locates item 572 in the domain name list, it can match it to item 572 in the list of IP addresses.

To do this, create two variables, one called “domain” and one called “index”.

Then, set the domain to be the answer of the ask block, and the index to be the integer 1.

A screenshot of the web version of Scratch 3. In the variables menu, two new variables "domain" and "index" are highlighted. In the script area, two new blocks have been added to those in the last image. The first says "set domain to answer". The second says "set index to 1".

Since your algorithm may need to search to the end of the list, you’ll need to use a loop that keeps going until the index is at the last item in the list.

Currently, the index is 1. So the algorithm needs to check whether the first item in the list matches the domain name the user is looking for. To do this, add a conditional if/else inside the repeat until loop.

A screenshot of the web version of Scratch 3. Underneath the previous blocks, a "repeat until index = length of Domain Names" block has been added. Inside this is an "if..else" block. The if statement in this block is "if item index of Domain Names = domain then".

If there is a match, then we know that the index of the IP address list will contain the IP address we’re looking for. So add a “say”, “join” and index blocks to display the result of the search.

A screenshot of the web version of Scratch 3. The previous code has been used. A "say" block has been inserted into the gap between "if" and "else". It reads "say join The IP address is item index of IP Addresses". If the domain name doesn’t match, we want the index to become 2, and the algorithm to go back to the start of the loop. So under your else, add a “change index by 1”.

A screenshot of the web version of Scratch 3. The previous code has been used. In the "else" gap is a block reading "change index by 1".

Now try out your code and see if it works. Pick out a random domain name from the list and use your algorithm to search for the IP address.

www.google.co.uk

216.239.32.20

Thoughts

  • How would you use this activity in a lesson with your students?
  • How could you change this code to return a “not found” message if the search term doesn’t exist in the list?

 

This article is from the free online

An Introduction to Computer Networking for Teachers

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education