Skip to 0 minutes and 2 seconds In this step, you’re going to have a go at building your own DNS server in Scratch. First, go to Scratch.
Skip to 0 minutes and 12 seconds Let’s try to type that correctly– scratch.mit.edu. And once we’re there, we’re going to start creating. If you’re logged in, you can give your project a name such as “DNS Server”. But otherwise, we’re just going to get started. The first thing we need is a list of domain names and IP addresses. So what we’re going to do is we’re going to go to our “Variables” selection, and we’re going to click on “Make a List”. Call the first one Domain Names. There we go. And we’re going to create a second list, which we’re going to call “IP Addresses”. Click OK. So now we have our two lists.
Skip to 0 minutes and 56 seconds We could sit and type in a few thousand IP address and domain names, but what we’re going to do instead is we’re going to import. And I’ve downloaded two files, which you will find in the text instructions for this step. And the first one I’m going to do is import domains.text. And here, we’ll see a whole list of domain names, and there’s 1,002 items in that list. Then, we’re going to do the same thing for IP addresses. So we’re going to go into IPs, open, and here’s a list of IP addresses. So now that we have these two things together, we’re going to program our Scratch script to do what’s called a linear search.
Skip to 1 minute and 37 seconds It’s going to search down the domain names for a URL that’s been entered. And then it’s going to find a corresponding IP address in the IP address list. And it’s a very simple search, it’s just going to search through that list of items one by one. So to do that, the first thing we’re going to do is we are going to grab an event. And we’re going to add a control block. And I’m just going to make this a little bit bigger so it’s nice and visible. Yep And then the very first thing we’re going to do is we are going to ask the user sort of what’s your name? We’re going to say enter a URL. OK.
Skip to 2 minutes and 16 seconds And that’s going to ask them a question. What we then need to do is we need to get that URL and store it somewhere. We’re also going to need a variable to track the position of our search. So we’re going to add two variables. The first one, so make a variable, is going to be called “domain”, and this is where the user’s answer is going to go. And our second variable is going to be called “index”. Now, index is the position of our search. And we are going to set domain to be the answer that they inputted. So I’ll ask the question. Whatever their answer was will get stored in domain.
Skip to 3 minutes and 0 seconds And then we are going to set our index to 1. There we are. So now we’re moving our script. What will happen is the Scratch Cat will ask us for a URL. It will set domain to be the answer and index to 1. Next, we want our script to loop through the list of domain names until it reaches the end. So here, we are going to get a “repeat until” block. And we are going to repeat until– let me get an equals operator from in here– and we’re going to say we’re going to repeat until our index variable is equal to the length of our list. So once our index reaches 1,002, this loop will stop.
Skip to 3 minutes and 56 seconds Next, we want to do a comparison between the item that the user told us, the URL, and the item in the list at the position index. So we’re going to use an “if” block to do that. And I’m going to use an “if then else” And the question that we’re going to ask is, “is the answer they gave that we’ve stored in domain, is that the same as list position 1 or 2 or 3 or 4, as we go through the list”? So we’re going to say is, and then we’re going to use an equals operator. There we go. We’re going to say is our domain, which is here, equal to.
Skip to 4 minutes and 42 seconds And then we’re going to get “list items at item”. Not one of domain names, but item “index” of domain names. So what this will do is it will look at position 1, to begin with, of domain names, compare it with domain. If they match, we are going to send a message. So we are going to say something. And in there, I am going to say– I’m going to say two things– I’m going to join together. Where are we looking here? There we go. We’re going to say “the IP address is”, and then we’re going to retrieve the IP address from the IP address list using our index position.
Skip to 5 minutes and 34 seconds So we’re going to say item, something from IP addresses, and that item is determined by the index number. So if we find it, tell us what the IP address was. If we don’t find it, if the item we’re currently looking at is not the correct item, then we are going to increase our index by 1 and therefore move on to the next item in the list. So hopefully this should all work. If we run this, our Scratch Cat says “Enter a URL”. We’re going to type in www.google.co.uk. When I hit Enter, it tells me that– well, I can see here that a position is 72. The IP address is, and that gives me an IP address.
Skip to 6 minutes and 26 seconds I could then, if I wanted to, take that IP address, take it and type it into a browser window. And I would go to Google without needing to know the name of the Google website. If I try a different web address– so let’s try “www.raspberrypi.org”. If I hit Enter now, I don’t get an answer, and I can see that it’s gone all the way to 1,002 without finding that domain name. So we’ve built ourselves a simple domain name server in Scratch. Now that we’ve done that, have a think about how you might use that activity with your students. Could you get them, for example, to extend the script to respond if no match is found?
Skip to 7 minutes and 9 seconds Share your thoughts in the comments section below.
Build a 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”.
Next, you’ll need to populate each list. About a 1000 entries in each should be enough.
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.
You should end up with something that looks like this.
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.
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.
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.
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.
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”.
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.
- 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?
Share your thoughts in the comments section.