Runtime Revolution
 
Articles Other News

Three Nifty Ways the Internet Can Help You Out

by Bill Marriott

One of the wonderful things about living in our connected world is the amazing number of services that exist on the Internet. You can get television listings, stock quotes, currency exchange rates, and news feeds. Between the embedded browser capability in Revolution 2.8.1 and later, and the handy URL keyword that's been around forever, it's easy to pull that information into your solutions and make use of it.

The examples I just listed can take a lot of processing to massage into a usable form. But in this article I wanted to highlight three bits of information you can harvest from the Internet that take almost no processing or reformatting at all, but are very handy.

Time

The old joke about a broken watch being useful in that it is precisely accurate at least twice a day reminds us of the simple fact that the system clocks on users' computers cannot be relied upon to be correct. Users may not enable the automatic time correction feature in newer operating systems. The clock can be set to the incorrect time zone or off by so much that the time cannot be automatically set. On older systems, a dead motherboard battery can cause the clock to "drift" especially when the system is powered down.

It's not such a huge problem when you're timing durations within the application. But if you're creating log files or developing solutions that involve multiple computers, the time variations can be significant. For these reasons, it is useful to "synchronize" clocks around a single standard.

The U.S. Navy maintains the "official" time for the United States. They present a web site which shows visitors the correct time in a variety of ways.

In Revolution, try this:

put URL " http://tycho.usno.navy.mil/cgi-bin/timer.pl "

You'll see the message box fill up with some very simple HTML that shows the current time as of the execution of the command. The UTC time is always line 6, which is preceded by the HTML tag for a line break and nothing else.

This makes it very simple to use in your scripts. When your application starts up, just grab this information and determine what the difference is between the user's system clock and the naval observatory time. Save this information whenever you are logging events or interacting with other systems, and you'll always be on the same page, without confusing time discrepancies.

Random Numbers

If you've spent much time at all in Revolution, you know that to simulate the throw of a die you use the expression, "put the random of 6" to get a number between 1 and 6.

There's a limitation in this, which is that numbers returned by this function are not random in the true sense of the word. They are "pseudo-random" because they are generated by algorithm. The formula which generates them simulates the appearance of randomness, but in fact the results are completely predictable if you know the random seed used to generate them. For example if you put:

set the randomSeed to 10

repeat three times

answer the random of 6

end repeat

You will see the response, 6, 5, 3 every single time . In fact, while you could generate millions of numbers this way and not see a pattern, eventually the sequence would repeat itself. This behavior is quite useful in debugging a program. And when the seed is not preset (or based on the system clock), the sequence of "random" numbers generated is perfectly usable for many purposes, such as games.

But what if you need to have a truly random number? Something which absolutely cannot be predicted and meets a high standard for statistical randomness?

For this we turn to www.random.org , a site created by Dr. Mads Haahr. This site not only explains the difference between pseudo-random numbers and true random number eloquently, but it also is a service which generates truly random numbers, based on fluctuations in the atmosphere of the Earth.

Going to the main index page shows a variety of ways you can play with random numbers via web interface. But for Revolution applications, we'll want to use an expression like this one:

put URL "http://random.org/sequences/?min=1&max=52&format=plain&rnd=new"

The items after the "?" are parameters to the site that describe the type of sequence you want to generate, and the format in which you want the results to appear. This particular command URL simulates the shuffling of a deck of cards. Run it and you will see the numbers 1 to 52 displayed in a truly random order in the message box, one per line. (Complete documentation of the possible parameters can be found at the site.)

When you know your users will be connected to the Internet, you can use this service to effectively generate a truly random number sequence, and parse it easily using Revolution's "line" chunk expression.

External IP Addresses

Most of us know that every computer connected to a network has an IP address. This address is like a telephone number that helps systems identify and communicate with each other.

There are a few different techniques which can help you determine the system's local IP address(es) but that isn't very useful when you're trying to communicate over the Internet. A "local" IP address is sort of like an extension within an office building. It could look like "10.0.0.15" or "192.168.2.3" and to complicate things further, a computer may have more than one depending on its hardware and software configuration.

The "public" or "external" IP address, however, is what systems outside of your local network will see, for example when you connect to a web server. A given system may not know what its external IP address is, given the number of devices which can sit between it and the outside world.

A full investigation of what IP addresses are is outside the scope of our discussion. But suffice to say, it can be tricky to figure out what your external IP address is without going out to the Internet yourself.

One popular service, www.myipaddress.com, is known by gamers and geeks worldwide as a very handy way to determine their public/external IP address. You could use the page shown by this service, but I don't recommend it. The formatting of the page changed periodically, and it's not designed to be a "service" like the other examples in this article. Instead, I suggest you make a very small text file containing the following text:

<?php echo($_SERVER['REMOTE_ADDR']."\r\n"); ?>

Name the file myip.php and upload it to a web server you have access to. It's simply a very simple PHP command that will output the IP address without any formatting to whatever calls it.

After you upload it, go to Revolution and try it out:

put URL " http://myserver.com/myip.php "

And voila! Revolution now knows your external IP address.

It's so simple to make use of information from the Internet within Revolution scripts. I hope this article has given you some ideas to use in your own applications.

 
©2005 Runtime Revolution Ltd, 15-19 York Place, Edinburgh, Scotland, UK, EH1 3EB.
Questions? Email info@runrev.com for answers.