Tag Archives: Console

It serves!

Writing a Working Web Server in Under 100 Lines of Code

UPDATE 12/30/12: You must run the compiled executable as an administrator on systems with UAC. Otherwise, the program will crash and exit.

I was reading some coding blogs last night and I happened across a guy who had written a minuscule web server in C# over his lunch break. I knew right away that I had to have one, too. A server like this one serves little practical purpose; it does not support any sort of server-side scripting (though adding a BASIC interpreter to it would be extremely cool). It does, however, serve a wide variety of documents (and will detect their MIME type prior to serving them.

The first thing I did was write a Log method to make console output look fancy:

static void Log(string message)
{
    Console.WriteLine("[{0}]: {1}", DateTime.Now, message);
}

That was quick. Next was to create an actual server method, which would continuously loop and be started asynchronously by the Main() method.

static void RunServer(int port)
{
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(string.Format("http://+:{0}/", port)); // Where to listen
    listener.Start(); // Fire up the server
    while (true)
    {
        HttpListenerContext context = listener.GetContext(); // Get a connection
        Log("Requested: " + context.Request.Url);
 
        if (context.Request.RawUrl.EndsWith("/")) // Just asking for index; redirect
        {
            context.Response.StatusCode = 301;
            context.Response.StatusDescription = "301 Moved Permanently";
            context.Response.RedirectLocation = context.Request.RawUrl + "index.html"; // Just send browser to index.html
            context.Response.Close();
            Log("Redirected client to /index.html.");
        }
        else
        {
            byte[] retVal;
            try
            {
                retVal = File.ReadAllBytes("www" + context.Request.RawUrl); // Give the client the requested page
            }
            catch
            {
                context.Response.StatusCode = 404; // File not found; tell the client.
                context.Response.StatusDescription = "404 Not Found";
                Log("404: " + context.Request.RawUrl);
                context.Response.Close();
                continue;
            }
            string mime = GetMimeType(context.Request.RawUrl);
            context.Response.Headers.Add(HttpResponseHeader.ContentType, mime); // Give response a MIME type
 
            Stream respStream = context.Response.OutputStream; // Response stream we'll write to
            respStream.Write(retVal, 0, retVal.Length);
 
            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "200 OK"; // Let client know stuff is okay
 
            respStream.Close();
            context.Response.Close();
            Log("Served: " + context.Request.RawUrl + " as " + mime + ".");
        }
    }
}

There’s quite a bit going on here; I’ll try to explain it all. The beginning of this method configures an HttpListener to wait for connections and starts it. It then starts an infinite loop (the user can stop the server by closing the console window) of continuously waiting for and serving requests (listener.GetContext() blocks until something makes a request). A client can ask for three things in this simple server (since we’re not running server-side scripts):

  1. A page that exists (e.g. /index.html)
  2. A page that doesn’t exist (e.g. __++++_____-sdasfash1234.abcdf)
  3. A directory root that may or may not exist (e.g. /mysite/)

The first thing we check for is the directory root case. We check the URL to see if it ends in a slash. If it does, we redirect the client to whatever the path is, plus “index.html.” We could simply serve the “index.html” off the bat, but we’re (poorly) code golfing right now! That would take more code and accomplish essentially the same thing!

If the RawUrl does not end in a slash, we’re serving a page! Anyway, the server stores all its files in a “www” folder that lives in the same directory as the executable. Fetching “www” plus the raw path with File.ReadAllBytes gets the data of the file we are looking for. The try statement is used in the event we can’t find the file. If an exception does get raised, the server returns a “404 File Not Found” error to the client. If this server were fancy, it would also serve a 404 error page.

If the server can find the file, it determines the MIME type through a simplistic process I will detail in a moment, sets the MIME type in the header, writes the data to the client stream, gives the client a “200 OK” (I am not sure if the order matters for all this, or if all the data simply gets flushed when the HttpResponse gets closed, but this code worked fine in Chrome and IE 9), and closes the connection.

The last thing is to determine the MIME type of the file. Rather than using WIN32 interop calls, I decided to write a simplistic method of my own based on the file extension being served.

static string GetMimeType(string fileName)
{
    // Fast way of determining if the lowercase file name ends with something
    Func<string, bool> fendw = new Func<string, bool>(inp => fileName.ToLower().EndsWith(inp));
 
    // Use our function to figure out the mime type
    if (fendw(".htm") || fendw(".html")) return "text/html";
    else if (fendw(".ico")) return "image/vnd.microsoft.icon";
    else if (fendw(".png")) return "image/png";
    else if (fendw(".jpg") || fendw(".jpeg")) return "image/jpeg";
    else if (fendw(".gif")) return "image/gif";
    else if (fendw(".js")) return "text/javascript";
    else if (fendw(".css")) return "text/css";
    else return "application/octet-stream";
}

This method wouldn’t be very interesting were it not for the Lambda Expression (remember those things I love?). The Lambda Expression takes as a string as an argument and returns a Boolean of whether the lowercase version of the provided file name ends with the specified text. In other words, it checks to see if the file has whatever extension you pass to it and ignores case. The “fendw” name, although cryptic, is short for “file ends with;” it prevented me from having to type fileName.ToLower().EndsWith(…) over and over again!

That’s it. The web server works — we just need to add a Main() method:

static void Main(string[] args)
{
    Console.WriteLine("On what port should the server run?:");
    int port = int.Parse(Console.ReadLine());
 
    Task server = new Task(() => RunServer(port)); // Start the server async
    server.Start();
    Log("Server started on port " + port.ToString() + ". Close this window to stop it.");
 
    while (!server.IsCompleted) ; // Wait infinitely
}

The first part of this method gets the port the user wants to run the server on. I used 2013, since it’s almost New Year’s! Next, it creates an asynchronous task (using a Lambda Expression again!) to run the server on, then fires it up and blocks indefinitely. The use of the Lambda expression is unnecessary here (since the thread Main() runs on will just loop forever), but if you wanted to listen on more ports, you could create more tasks to run new instances of the server on. It makes the code more extensible, right?

It serves!

It serves!

Serving a real web page from a website I built for a charity organization last year!

Serving a real web page from a website I built for a charity organization last year!

If you desired, you could actually open a port in your router and let your friends connect to your cool new server. I should probably warn you that doing so would be a terribly risky idea, even if you only have the port open for a short while. You wouldn’t run through wolf-infested woods with a steak or swim at the Great Barrier Reef with a gaping leg wound for ten minutes, so why would you do the same on the Internet? In other words: this code is provided “AS-IS” with no express or implied warranty, especially since I do not really know how secure this server is. My guess is that it isn’t. Still, it’s hard to go wrong with a 100-line server!

Calculating Pi

One programming task that I had never taken on (up until this morning, anyway), was a pi calculator. Finding pi is not an overwhelmingly daunting task; one can get an extremely precise approximation from the System.Math.PI constant. However, that’s no fun. The real fun is in rolling your own pi calculator. As it turns out, this was no beginner project. It was math-heavy, theory-heavy and CPU-heavy. Oh, and it was a blast!

The trouble was that at first, I had no idea where to begin. In C#, there is no “arbitrary precision” decimal type; the decimal class can hold 28 or so significant figures (remember the calculator tutorial?). No self-respecting pi calculator would ever stop at 28 significant figures. 28,000 would be a bit better! The first challenge in coding the pi calculator would be coming up with a way to store an unlimited number of significant figures. I’d read about some pi calculator authors implementing “BigFraction” or “BigRational” classes based off of the fairly new BigInteger (arbitrarily-sized .NET integer) class, so I thought I’d take a crack at writing one of those. I also decided that before I wrote a single line of code, I needed to choose a pi calculation formula.

The requirements for a formula are simple: that it converges relatively quickly and requires relatively few calculations per iteration. Unfortunately, the use of a “BigRational” class adds another requirement: no fractional powers of numbers that have irrational fractional powers. That’s a real drag, because it means we can’t use the Chudnovsky Algorithm, which is the Holy Grail of fast pi calculation algorithms:

The trouble is with the 3k + 3/2 part; 640320 is not a perfect square (it is almost 800.2^2, but not quite).

The trouble is with the 3k + 3/2 part; 640320 is not a perfect square (it is almost 800.2^2, but not quite).

With the really good algorithm out of the way, I started trying to find another good algorithm. My first thought took me back to my trig identity days a few years ago (okay, the past three years of high school):

So then if you take the deriv... actually, no.

So then if you take the derivat… actually, no.

This is actually a sound, correct thought. In fact it has a name: the Gregory-Leibniz formula. This will eventually converge to pi. However, after following a link off the handy, dandy Wikipedia page I used to decide which algorithm I would use in my program, I learned that it takes five billion iterations of arctan(1) (which I will get to in a moment) to produce ten correct digits of pi. YIKES! It was a good thought.

Anyway, I eventually decided on the original Machin formula, from 1706:

Although it was created in 1706, Wikipedia says it is still one of the fastest-converging pi-calculation algorithms!

Despite its age, it converges quickly and is relatively easy to compute.

Awesome. We have a formula. Now we need to figure out how an arctangent can be computed. It is actually rather simple, especially with programmatic help:

See. Not that bad.

See. Not that bad.

Alright! We have the formula for computing pi and we have the formula for computing arctangent, so now we’re done. Not so fast. Remember when I mentioned the BigRational class? As it turns out, writing a BigRational class is the bulk of this project. I won’t bore you with too many tiny details of the way I implemented it; I will tell you that it was designed for simplicity over speed. It definitely needs some refining, but it works.

The basic concept behind the BigRational class is simple: it is a fraction. It has a numerator and a denominator. The numerator and denominator are arbitrarily-precise, so the entire ratio is arbitrarily-precise. The class has a bunch of methods to help the numerator and denominator do their fraction-y duty:

As it turns out, a lot of help.

As it turns out, a lot of help.

Most of the methods listed in the class diagram are pretty self-explanatory; I won’t go into detail here (but you can download the source code to the project at the bottom of the page). All the operators help the BigRationals play well together, and the * operator has an overload that allows “scalar” multiplication. That is, it allows multiplication by a BigInteger.

The two methods that I will delve into are the Reduce method and the ToDecimalString method. The Reduce method reduces a BigRational into its simplest form. It implements Euclid’s Algorithm, which is a means of reducing a fraction. It works in steps:

  1. The larger of the numerator and the denominator is set as the “big number.” The smaller is set as the “small number.”
  2. The program finds the remainder when the big number is divided by the small number.
  3. If the remainder is not zero, the small number becomes the big number and the remainder from step two becomes the small number.
  4. If the remainder is zero, both the numerator and denominator may be divided by the small number. At this point, the small number is the greatest common factor of the numerator and denominator.
  5. Steps 2, 3 and 4 are repeated until step four terminates with the small number equal to one. At this point, the fraction is in simplest form.

In code, the above steps look like this:

public void Reduce()
{
    // Set up big and small number
    BigInteger bigNumber, smallNumber = 0;
    if (Numerator > Denominator)
    {
        bigNumber = Numerator;
        smallNumber = Denominator;
    }
    else
    {
        bigNumber = Denominator;
        smallNumber = Numerator;
    }
 
    // Now divide a bunch of times
    while (smallNumber != 1)
    {
        // Find the remainder
        BigInteger rem = (bigNumber % smallNumber);
        if (rem != 0)
        {
            bigNumber = smallNumber; // Set up the numbers for the next iteration
            smallNumber = rem;
        }
        else
        {
            // We can divide both the numerator and denominator by the previous remainder
            Numerator /= smallNumber;
            Denominator /= smallNumber;
 
            // Re-assign bigNumber and smallNumber
            if (Numerator > Denominator)
            {
                bigNumber = Numerator;
                smallNumber = Denominator;
            }
            else
            {
                bigNumber = Denominator;
                smallNumber = Numerator;
            }
        }
    }
 
    // One last thing -- if the numerator is positive and the denominator is negative, the numerator needs to become negative and the denominator needs to become positive.
    // If they're both negative, they should both be positive
    if (Denominator < 0 && Numerator > 0 || Numerator < 0 && Denominator < 0)
    {
        Numerator = -Numerator;
        Denominator = -Denominator;
    }
}

The only change between the pseudo-code and actual implementation was the addition of the small if-statement at the bottom. I noticed that the denominator would sometimes be negative instead of the numerator being negative. That would probably throw off some calculations, so I made sure that only the numerator of the BigRational could ever be negative.

The ToDecimalString method converts a BigRational into its decimal equivalent — a handy helper when it comes to calculating pi (since it’s no fun unless it starts with 3.14159…). I considered two different implementation styles:

  1. Multiplying the entire fraction by 10^however many digits I wanted.
  2. Creating my own long division algorithm that would return a string of digits of a specified length.

Contrary to what seems sane, I chose the second method. I suppose it was partially because I had always wanted to write an actual long division algorithm, but by any means, it took some head-scratching at first. Long division isn’t something I have to do very much, so I started by doing out a few long division problems. As it turns out, there is a nice pattern. The non-decimal part of the number may be calculated in one fell swoop by using integer division. It may then be committed to a string. After that, a decimal point should be committed to the string. Then, a “new numerator” should be created. Its value is the remainder from the previous iteration multiplied by ten. The next decimal digit is equal to the “new numerator” minus the remainder when it is divided by the denominator of the fraction, all divided by the denominator of the fraction. This process can be repeated until the desired number of digits is reached.

Okay, it makes more sense in code. Wow.

public string ToDecimalString(BigInteger decimalDigits)
{
    StringBuilder rv = new StringBuilder();
    this.Reduce(); // Go as fast as possible
    BigInteger remainder = Numerator % Denominator;
 
    // Get the non-decimal part
    rv.Append(((Numerator - remainder) / Denominator).ToString() + ".");
    remainder = Numerator % Denominator;
 
    BigInteger newNum = remainder * 10;
 
    // Now get the decimal part
    for (BigInteger i = 0; i < decimalDigits; i++)
    {
        rv.Append(((newNum - (newNum % Denominator)) / Denominator).ToString()); // This literally just does long division
        newNum = (newNum % Denominator) * 10;
    }
 
    return rv.ToString();
}

The only real advantage this method presents is the lack of any string parsing. It is all basic use of a StringBuilder. Plus, it seems to work very well!

With the BigRational class done, the next step was the actual pi calculation. The first step was to implement an arctangent method, in accordance with the formula I mentioned earlier.

public static BigRational ArcTangent(BigRational input, BigInteger iterations)
{
    BigRational retVal = input;
    for (BigInteger i = 1; i < iterations; i++)
    {
        // arctan(x) = x - x^3/3 + x^5/5 ...
        // = summation(n->infinity) (-1)^(n) * x^(2n+1)/(2n+1)
        BigRational powRat = input.Pow((2 * i) + 1);
        retVal += new BigRational(powRat.Numerator * (BigInteger)Math.Pow(-1d, (double)(i)), ((2 * i) + 1) * powRat.Denominator);
        if (i % 100 == 0)
        {
            Console.WriteLine("ArcTangent {0}: {1}/{2} iterations complete.", input, i, iterations); // Status update.
        }
    }
 
    return retVal;
}

Wait. There’s a parameter called “iterations.” How do we figure out how many iterations are necessary? This page provided the answer. As it turns out…

I find it's a good idea to add twenty or so digits to the end to make sure everything is correct.

I find it’s a good idea to add twenty or so digits to the end to make sure everything is correct.

In other words…

public static BigInteger GetConversionIterations(BigInteger digits, BigRational q)
{
    return (BigInteger)((double)digits / (2 * Math.Log10((double)q.GetReciprocal())));
}

That was quick. That also finished laying the foundation for actually calculating pi. Phew. That was a lot of keystrokes.

public static BigRational GetPi(BigInteger numDigits)
{
    // pi = 16 arctan(1/5) − 4 arctan(1/239)
    BigRational oneFifth = new BigRational(1,5);
    BigRational oneTwoThirtyNine = new BigRational(1, 239);
    BigRational arcTanOneFifth = PiCalc.ArcTangent(oneFifth, PiCalc.GetConversionIterations(numDigits + 1, oneFifth)); // Start computing
    BigRational arcTanOneTwoThirtyNine = PiCalc.ArcTangent(oneTwoThirtyNine, PiCalc.GetConversionIterations(numDigits + 1, oneTwoThirtyNine));
 
    return (arcTanOneFifth * 16) - (arcTanOneTwoThirtyNine * 4);
}

This code block implements Machin’s formula as discussed above. An improved version of this program could take advantage of multi-core computing by calculating one arctangent on one thread and the other arctangent on another. More formulas should also be tested to determine which is the fastest; this program really slows down as the iteration count gets higher because the fractions get really, really, really huge. Of course, this program was never going to break any records anyway because it was written in C#. JIT-compiled, super high-level languages aren’t exactly speed demons. Then again, my Phenom II x4 (overclocked to 3.7 gHz) is also beginning to show its age. I’d be curious to see how performance compares on one of the latest-generation i7s.

Although it is sluggish, it does seem to compute pi properly. Here are the first 10,001 digits:

Although it looks like a lot of pi, it's not a lot of pi.

Although it looks like a lot of pi, it’s not a lot of pi.

So that’s it. It’s done. It works. Now it just needs to run for a few days so I can brag to my friends! The funny thing about calculating pi is how although we believe pi is completely irrational and will never end, we’ve calculated enough digits that it might as well end. If my calculations are correct, it only takes about 80 digits of pi to calculate the volume of the observable universe to the nearest cubic meter. Thus, ten thousand would calculate it to the nearest 1/10^9920 cubic meter, whatever that would be. That’s some mind-blowing precision. Imagine what a million digits would do.

Download this Project (MIT License)

Quotebook, a Grotesquely Over-complicated Version of a Program I Wrote for a Quiz

Last week in my video game development class (which is an introductory-level course on Python 3 with some focus on games), we had a quiz. We had to talk about some things we’d learned, describe the intricate differences between Python’s if, elif and else statements, then write a program. The program intrigued me: the goal was to “simulate a fortune cookie” and display one of five different, pre-determined strings at random.

My first reaction was relief that I was going to be able to write the program with little issue. My second reaction was how incredibly far I could take that idea. An entire social network materialized in my head; a place where people could go to share and discuss quotes or fortunes. Then I realized that I was crazy, but I did decide to make some changes to the program we’d written in my spare time:

  1. Create an online component that would allow anyone to submit quotes to the “fortune cookie”. The “cookie” has become a system that I call Quotebook, for lack of a better name.
  2. Create a Python client for some kind of bare-bones Quotebook API that would accomplish the same task as the quiz program, except after retrieving the quote from Quotebook.

I knew the Python part would be the easy part, so I fired up Visual Studio and got cracking. I used most of the default template but changed the top text and removed the menu. I kept the HeadLoginView and all the other assorted login-related items so I could use the Membership class to attribute submitted quotes to a user. I also added a database to the project and created a table called quotes. Quotes has four columns:

  1. id (int)
  2. byUser (nvarchar)
  3. timestamp (timestamp)
  4. quoteText (nvarchar)

On the homepage, I added a big TextBox and a submit button. Then, I wired it up to write to the database:

protected void btnSubmitQuote_Click(object sender, EventArgs e)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated) // Make sure user is authed before writing bad things to DB
    {
        // Connect to the DB
        SqlConnection sconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["messageDB"].ConnectionString);
        sconn.Open();
 
        // Create the command to insert the values into the database
        // PARAMETERS are used to STERILIZE all inputs.
        SqlCommand scomm = new SqlCommand("INSERT INTO quotes (byUser, quoteText) VALUES (@byuser, @quotetext)", sconn);
        scomm.Parameters.Add(new SqlParameter("@byuser", System.Data.SqlDbType.NVarChar));
        scomm.Parameters.Add(new SqlParameter("@quotetext", System.Data.SqlDbType.NVarChar));
        scomm.Parameters["@byuser"].Value = Membership.GetUser().UserName;
        scomm.Parameters["@quotetext"].Value = tbQuote.Text;
 
        // Commit to DB
        scomm.ExecuteNonQuery();
 
        // Close and refresh page.
        sconn.Close();
        Response.Redirect("Default.aspx");
    }
    else
    {
        lblError.Text = "
 
You must login or register before adding a quote!"; // Alert user
    }
}

There are a lot of points to cover here. First, the user state gets checked so quotes don’t get attributed to some kind of phantom (and so no exceptions get thrown!). I added a red, textless label to the area next to the submit button so that if the user isn’t logged in, he will get an error message. If he is logged in, the quote gets written to the database. I had to provide my own connection string in the web.config file. The connection string gets used to connect to the database, and a command gets created on that connection.

The command has two parameters: the text of the quote and the user’s name. Those are set through SQL parameters. Although I have no plans to SQL injection attack my own database, it is good practice to use parameters whenever user input is to be committed to a database. After the parameters get set, the data gets written and the connection gets closed.

With the input side of the Quotebook I/O taken care of, I set out to write the output. I wanted two different kinds of output: a mess of all the quotes in reverse chronological order on the homepage and the “bare-bones API” I mentioned earlier. Before I wrote either, I created an easy way to get data out of the database that I call the QuoteEnumerator class. I created a struct called Quote, which contains the quote text and the name of the quote submitter. QuoteEnumerator has a static method that returns a list of all the Quotes stored in the database. I implemented the two like this:

/// <summary>
/// Reads the DB and creates lists of quotes.
/// </summary>
public class QuoteEnumerator
{
    /// <summary>
    /// Obtains a list of quotes from the ASP.NET database.
    /// </summary>
    /// <returns></returns>
    public static List<Quote> GetQuotes()
    {
        List<Quote> retval = new List<Quote>();
 
        // Connect to DB and set up the query
        SqlConnection sconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["messageDB"].ConnectionString);
        sconn.Open();
        SqlCommand scomm = new SqlCommand("SELECT * FROM quotes", sconn);
 
        // Read the database
        SqlDataReader sdr = scomm.ExecuteReader();
        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                // Iterate through the results
                Quote q = new Quote();
                q.QuoteSubmitter = sdr["byUser"].ToString();
                q.QuoteText = sdr["quoteText"].ToString();
 
                retval.Add(q);
            }
        }
 
        return retval;
    }
}
 
/// <summary>
/// Contains a quote once it is extracted from the DB.
/// </summary>
public struct Quote
{
    public string QuoteText { get; set; }
    public string QuoteSubmitter { get; set; }
}

The QuoteEnumerator.GetQuotes() method is a relatively simplistic database reading method. It utilizes a SqlDataReader to iterate through all the rows. For each row, it retrieves the quote text and quote submitter, then adds them to a list containing all the other quotes. It returns that list. Using GetQuotes made implementing the homepage side of the output easy.

// Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    foreach (Quote q in QuoteEnumerator.GetQuotes())
    {
        // Append the quote to the page in a pretty-looking (?) way
        lblError.Text += string.Format("<div style='display:block; padding:2px; margin:0 auto; border:1px solid black; width:80%; text-align:center;'>{0}<br/><br/><span style='font-size:smaller'>Submitted by {1}</span></div><br>", q.QuoteText.Replace("\n", "<br/>"), q.QuoteSubmitter);
    }
}

The result is, uh, kinda stunning…

Quotebook Image

That comment was sure telling the truth!


I only had two obstacles left: implementing the API and implementing the Python script that would utilize it. The API came first. I created a page called getmessage.aspx and deleted all of the code from the ASPX page except the top line indicating that it was, indeed, ASPX. In the code behind, I came up with this little gem to write a random quote as a string to the page (and to give credit to the submitter):

protected void Page_Load(object sender, EventArgs e)
{
    // Grab a random quote
    List<Quote> allQuotes = QuoteEnumerator.GetQuotes();
    Quote chosen = allQuotes[new Random().Next(0, allQuotes.Count)];
 
    // Write the quote as the response text
    Response.Write(chosen.QuoteText + "\n\nSubmitted by " + chosen.QuoteSubmitter);
}

Lastly, I created a Python script to read from the API. It uses urllib, and given my limited Python knowledge, it is more than good enough! (Keep in mind that this is Python 3; my last Python post was written in Python 2)

# randomMessage.py
# Random message downloader python script
print("Fortune Cookie")
DLURL = "http://localhost:6642/getmessage.aspx" # Replace with yours
 
import urllib.request
while input("Enter Q to quit or press ENTER to continue: ").lower() != "q":
    # Open the API page
    response = urllib.request.urlopen(DLURL)
    data = response.read().decode("utf-8") # Download and decode to a string
 
    print("\n" + data, end="\n\n")

With that, I entered some quotes and fired my creation up!

Quotebook Python Script Running

The awesome thing about Python is how incredibly easy it is to build relatively useful programs like this one.


Sure, it’s seven or eight times more code than the school version, but it’s extensible.

Do you have any improvements for this? Additional features? Let me know!

Porting my Calculator to Python

Although the first language I actually learned was VB.NET, Python is what actually inspired me to begin programming. My dad talked me out of learning it (one of my friends suggested that I did); who knows what kind of programmer I would be today if I had learned it. My guess is that I would be better with server-sided activities and worse on the desktop. Perhaps I’d be more of a Linux guy.

Anyway, I got inspired to learn Python yesterday for whatever reason, and today, I decided to port my calculator to Python. For the sake of comparison, I stayed fairly true to my original C# program. Here’s the very annoying program I came up with (it has no way to exit and loops forever so you have to close the console if you want to close it):

# Python calculator program
# This is the first Python program I've written for a LONG time.
def main():
	while 1==1:
		try:
			number1 = float(raw_input("Enter a number, then hit enter: "))
			number2 = float(raw_input("Enter another number, then hit enter: "))
			operator = raw_input("Which operation do you want? (Choose +, -, * or /, then hit enter): ")
			if operator == "+":
				print "The sum is:", Add(number1, number2)
			elif operator == "-":
				print "The difference is:", Subtract(number1, number2)
			elif operator == "*":
				print "The product is:", Multiply(number1, number2)
			elif operator == "/":
				print "The quotient is:", Divide(number1, number2)
			else:
				print "Please choose another operator."
 
		except:
			print "Error, please try again."
 
		print "\r\n"
 
def Add(num1, num2):
	return num1+num2
 
def Subtract(num1, num2):
	return num1-num2
 
def Multiply(num1, num2):
	return num1*num2
 
def Divide(num1, num2):
	return num1/num2
 
main()

Here it is in action, running in PowerShell:

Python Calculator Image

You can do the four most basic operations over and over! Woo!

In doing this port, I noticed that my beginner-targeted C# code was way longer than it needed to be (with all the helper methods) but very easy to understand. I also noticed a lot of things about Python, some of which I liked and some of which I did not.

The Good

  • The console print command makes it very easy to concatenate variables in.
  • In the small program I had, relying on indentation instead of { } to organize code saved a lot of screen space.
  • The raw_input() function serves as an equivalent to Console.WriteLine(), Console.ReadLine() in C#, saving more keystrokes.
  • Overall simplicity and intuitiveness. I’m a C# developer, not a Python developer, but I was able to complete this port with a little help from Google. Python operators were similar to the C# ones and the syntax isn’t difficult to understand at all. I can read Python code and understand much of what it does, unlike Ruby programs which tend to make my eyes glaze over.

The Not-So-Good

  • I’m not sure how well the whole indentation thing would work in a huge program. Things are bound to get mixed up eventually. Still, I’ve spent plenty of time counting curly brackets trying to figure out why my code won’t compile, so it may just come down to preference.
  • Lack of strong typing. I’m used to seeing float number1, not just some random variable declaration. Again, things could get confusing, especially if you forget what a variable is for.
  • The variable declaration syntax. It makes copy and pasting code much easier not having to get rid of keywords, but it is nice to know that you are declaring a variable for the first time, even if it is with the var keyword in JavaScript.
  • This page says, “Java is a pair of scissors, Ruby is a chainsaw.” That’s fair enough, but where does Python figure into things? Does it have enough horsepower to get things done or should I stick with my C#?

Even with its flaws, I do rather like Python. XKCD sums it up pretty well. I think I will try writing some more programs in it. Heck, I might do some server-sided things with it. Learning another programming language can’t possibly be a bad thing.

.NET Applications: Open Source by Default

If you have been developing .NET applications for much time, I hope you have come to realize that your Windows applications are not very secure. What do I mean by that? Let’s take a very simple application as an example:

using System;
 
namespace InsecureApp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
            {
                Console.WriteLine("Enter your password:");
                if (Console.ReadLine() == "secret")
                {
                    Console.WriteLine("Logged in. My social security number is 123-45-6789.");
                    Console.WriteLine("Your session has expired. Logged out.");
                }
                else
                {
                    Console.WriteLine("Incorrect password.");
                } 
            }
        }
    }
}

I realize this example is trivial, but I am trying to illustrate a point. I’ll do it in two ways: first, using ildasm.exe and second by using Telerik JustDecompile. No, this isn’t a product endorsement. I just needed to find a free decompiler.

You can run IL DASM with the Visual Studio Command Prompt. Here’s the output from my test program:

IL DASM Example Image

Please don’t sign me up for too many credit cards!

Not that scary, right? Potential crackers can only see the login password and your social security number. In an actual application, an email address, password, or authentication token could be visible in plain text. Remember, I did this with a free tool. I didn’t even have to download anything! What if I did, you ask?

Decompiled Code

Oh, bother.

Not only is the code reasonably close to what it actually was (to the point where it is easy-to-understand), I could look at the code in VB.NET if I wanted to and there is syntax highlighting! This is another free tool that you can use without even understanding CIL. But, say you did. Back in the day (and I do mean “in the day”, perhaps four or five years ago), I became interested in what was going on behind-the-scenes in .NET and learned some CIL. I also learned that you can use /OUT=”location” with IL DASM to actually output CIL to a file. Oh, I forgot to mention – there is a corresponding IL ASM to put the program back together.

Let’s say I’m a lazy cracker, so I am going to just jump to being logged in and then never log out. I changed the CIL for Main to look like this:

  .method private hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    // Code size       81 (0x51)
    .maxstack  2
    .locals init ([0] bool CS$4$0000)
 
	ldstr "Cracked by 1337 H4x0r or something"
	call void [mscorlib]System.Console::set_Title(string)
 
    IL_0026:  ldstr      "Logged in. My social security number is 123-45-6789."
    IL_002b:  call       void [mscorlib]System.Console::WriteLine(string)
 
	call string [mscorlib]System.Console::ReadLine()
	pop
	ret
  } // end of method Program::Main

I then reassembled my program and ran it. There was no need to log in, and the program conveniently waited for me to hit enter before closing!

Simple Program Cracked

It took a surprising number of tries to assemble this application properly.

So, what can you do? Can you strong name your assembly? Sure, but it won’t do much to anyone that knows his or her stuff. Can you obfuscate it? Sure, but it’s only a roadblock. I did some reading about protecting .NET Windows applications from piracy, and the consensus seems to be that much protection isn’t worth it. Would you rather spend your time keeping people that will not pay for your software from using it or adding features to make more people willing to pay for it?

The important thing to take away from this post is to realize that anyone can see and modify what you write. You should not handle tasks that should be secure (such as anything that requires password or key storage) on the client side. Server-sided code is much better suited to secure tasks (since you cannot download ASP.NET binaries from a server unless something is seriously wrong), so anything that has to remain a secret should remain remote. Remember, transmitted data can be intercepted, so I am not telling you to send your passwords from the server to the client. I am telling you to have the client ask the server to do its dirty work.

The bigger message I wish to convey, though, is that you should minimize the amount of data that needs to be protected.

 

Will it Rain this Weekend?

While browsing MIT+K12 open projects, I came across an entire course proposal about probability. Now, I have very little formal probability training outside of one unit in precalculus last year, but I was intrigued by the idea of writing a program to simulate 10,000 trials. Obviously, 10,000 trials isn’t enough to get a really good data set so I added a few zeros to my C# for loop and ran 1,000,000 trials.

So, in keeping with the MIT course proposal, I checked the forecast for the weekend and found out that this weekend, the chance of rain on Saturday is 10% and the chance of rain on Sunday is 30%. My first reaction was mild aggravation because I went camping last weekend in the rain and I simply could’ve waited a week. My second reaction was, “Alright. Let’s get started.”

Probability Tree Image

Mr. Whalen, I hope you’re proud of me.

The next step was to write a program to test my calculations. I came up with a program structure that I hope is “random” enough. I am going to use System.Random instead of RNGCryptoServiceProvider because it is way faster and I believe it fine for something this simple. When I originally wrote this program, I used lists, populated them with true or false values, randomized them, and picked from them over and over again. I am not sure what kind of awful state of mind I was in when I wrote that code (whatever it was, there wasn’t enough caffeine), but I’ve come up with a way more elegant solution – Random.NextDouble(). If the double is less than the probability of rain, it rained. Otherwise, it didn’t. We also need three counters – one for when it rains sometime during the weekend at least once, one for when it rains exactly once and one for when it rains twice.

There is one other case: when it doesn’t rain, but 1,000,000 – the number of times it rained at least once takes care of that. Some operators make determining which case we’re dealing with easy – XOR is true if exactly one of two cases is true, OR is true if at least one case is true, and AND is true if both cases are true.

With that taken care of, all that was left was to write the code in a console application:

static void Main(string[] args)
{
    double probRainSat = 0.1;
    double probRainSun = 0.3;
 
    int rainyWeekends = 0;
    int oneDayRainyWeekends = 0;
    int bothDayRainyWeekends = 0;
 
    Console.WriteLine("Simulating one million weekends...");
    Console.WriteLine();
 
    Random rand = new Random(Environment.TickCount);
 
    // one million trials
    for (int i = 0; i < 1000000; i++)
    {
        if (i % 100000 == 0)
        {
            Console.WriteLine("{0}...", i);
        }
 
        bool rainedSat = rand.NextDouble() < probRainSat;
        bool rainedSun = rand.NextDouble() < probRainSun;
        if (rainedSun || rainedSat)
        {
            rainyWeekends++;
        }
        if (rainedSat ^ rainedSun)
        {
            oneDayRainyWeekends++;
        }
        else if(rainedSat && rainedSun) // We can use else if because it can't rain on exactly one and both days.
        {
            bothDayRainyWeekends++;
        }
    }
 
    Console.WriteLine("Rain on only 1 day: {0}/1,000,000 ({1})", oneDayRainyWeekends, oneDayRainyWeekends / 1000000);
    Console.WriteLine("Rain during the weekend: {0}/1,000,000 ({1})", rainyWeekends, rainyWeekends / 1000000);
    Console.WriteLine("Rain on both days: {0}/1,000,000 ({1})", bothDayRainyWeekends, bothDayRainyWeekends / 1000000);
    Console.WriteLine("Rain on neither day: {0}/1,000,000 ({1})", 1000000 - rainyWeekends, (1000000 - rainyWeekends) / 1000000);
    Console.ReadLine(); // Keep the program from closing.
}

Now you can run your new creation:

Probability Simulator Results

You just simulated about 19,200 years worth of weekends.

Not bad, huh? If you round, you’re spot on with the calculations. The math predicted almost exactly what the program would do. That, my friends, is pretty cool.

Creating a Calculator with C# Part 2: Creating Variables, Getting User Input, Doing Math, and Handling Exceptions

Welcome back. Hopefully the last tutorial on C# sparked your interest. If it didn’t, then I commend you for still giving this tutorial a shot. We’re going to learn how to use variables, get input from the console, and then conduct mathematical operations on the input. We’ll even display the result. Yes, we’re building a simple adder.

To begin, either create a new project or (gasp!) delete your Hello World project. I recommend the former; you probably haven’t been able to show it to your grandkids yet. Then, in the main method (just like before), we’re going to declare two variables: number1 and number2. How do you declare variables, though? Like this:

[type] variableName;

What’s [type]? Type is class the variable is. C# is called a strongly-typed language because you can’t mix and match types. If you declare a variable as an integer (using “int” or “System.Int32″ or “Int32″ as the type), you can’t put a string in it. This prevents errors down the line and encourages you to code better. Now, declare those variables!

static void Main(string[] args)
{
    int number1;
    int number2;
    // More code goes here!
}

This also seems like a good time to tell you two more things about C#:

  1. Every code statement you write (like int number1) has to end with a semicolon. Semicolons are so important that you can have a line containing nothing but a series of semicolons and your project will run properly! Try it if you don’t believe me.
  2. The line beginning with // is called a comment. Comments are ignored by the compiler and contain information to make your code easier to read. You don’t need a semicolon after a comment. C# also has what are called block comments. Block comments start with /* and end with */. Everything between the start and end is ignored. No matter how nicely you ask the computer to work in your comments, they will be ignored.

So, we’ve got our variables. We’ve got more knowledge about the C# syntax. Let’s fill those variables with numbers. Remember Console.WriteLine() and Console.ReadLine()? We’re going to use them again. We’re also going to use another method called int.Parse() which takes a string as an argument. Recall that I told you C# is strongly typed. The Console.ReadLine() method returns a string value, even if the user types in numbers. The int.Parse() method converts the numbers a user types in to a real integer. Here we go.

static void Main(string[] args)
{
    int number1;
    int number2;
 
    Console.WriteLine("Enter a number, then hit enter:");
    number1 = int.Parse(Console.ReadLine());
 
    ; ; ; ; ; ; ; ; ; ; ; ; ;
}

Just for fun, I threw in a line of semicolons; semicolons are awesome anyway. We’ve seen Console.WriteLine() before, but look at the line below that. Console.ReadLine() returns a string, and we’ve used it as the string argument for int.Parse. The value the user enters will be passed to int.Parse, and then the integer version is stored in number1. Now let’s take care of the other number.

static void Main(string[] args)
{
    int number1;
    int number2;
 
    Console.WriteLine("Enter a number, then hit enter:");
    number1 = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number, then hit enter:");
    number2 = int.Parse(Console.ReadLine());
}

The two lines we’ve added are identical to the two we added before. Next, let’s do something with our new-found numbers. We’re going to add them together and write them to the console. Let’s create a new variable called sum and assign it to the sum of number1 and number2. C# spoils you because it follows order of operations, unlike other languages I post about like PBASIC. The addition operator in C# is +, and using it is rather intuitive:

static void Main(string[] args)
{
    int number1;
    int number2;
 
    Console.WriteLine("Enter a number, then hit enter:");
    number1 = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number, then hit enter:");
    number2 = int.Parse(Console.ReadLine());
 
    int sum = number1 + number2;
}

We’ve created another variable called sum and immediately assigned it to the sum of number1 and number2. Sum is an int (32-bit integer – .NET has loads of other numbers like 8, 16 and 64-bit integers, decimals, and single and double-precision floating points), so we make sum an int as well. If you wanted to use multiplication (*), subtraction (-), division (/), or any other operation, you would substitute the corresponding operator in for the plus sign. Now, The last thing we have to do is print the sum to the console and wait for the user to exit.

static void Main(string[] args)
{
    int number1;
    int number2;
 
    Console.WriteLine("Enter a number, then hit enter:");
    number1 = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter another number, then hit enter:");
    number2 = int.Parse(Console.ReadLine());
 
    int sum = number1 + number2;
    Console.WriteLine("The sum is: " + sum.ToString());
    Console.ReadLine();
}

What on earth is this ToString() thing? And why is there a plus sign on the string!? I thought C# was strongly typed, and the + goes with numbers!! Whoa there. I’m glad you’re thinking about this typing thing; it will save you a lot of headaches later on. We’ll start with the plus sign. + has another purpose besides addition: concatenation. Concatenation is the process of appending one string on the end of another. If you were to concatenate “hello” and “world” by writing “hello” + “world”, the result would be “helloworld”.

The ToString() thing should make a little more sense. Remember that sum is an integer, not a string. Calling ToString() lets us use it as a string. Every single class you can declare as a variable in the .NET framework has a ToString() method. Even System.Object, the basis for every other class in .NET (if you want to understand this better, look up inheritance. It’s sort of an advanced topic but there’s nothing wrong with being exposed to advanced topics!) has a ToString() method. Return values vary; ToString() tends to work well with numbers and poorly with more complicated classes like System.Windows.Forms.Form. Anyway, back on track – once sum is usable as a string, we append it to the end of the other string. Go ahead and run your code by hitting F5.

Adder Program Console Image

Yay! It works!

We only have one topic left to cover in this lesson: handling exceptions. What’s an exception? Exceptions are how your program handles errors. Errors happen all the time. Even the almighty Google has to handle errors.

Google Exception Image

I wonder why that page can’t be found?

In this case, Google’s server code searched for whatever file would normally be served for google.com/the long file name that doesn’t exist, and didn’t find it. In .NET, a FileNotFoundException would result. When an exception occurs, it has been “thrown”. If an exception is thrown and not caught, it will cause your program to come crashing down, just like a baseball that isn’t caught. You might be thinking that this program is way too simple to have to worry about exceptions, but every application that handles user input should have very careful exception handling because to write bulletproof code, you have to assume your users are less technically competent than you are. You have to assume they don’t know what they’re doing and will send you angry emails about how your application is full of bugs. Let’s pretend to be a dumb user right now. Run your application again (close it then hit F5 in Visual C#), and type in some gibberish instead of a number.

Exception in Visual C# image

You don’t have to send yourself any angry emails.

Congratulations, you’ve just seen your first exception. If you haven’t followed the code here exactly, it may not be your first. The question is, how do you keep your program from crashing when a user puts random input is? You can encapsulate your code in a try, catch block. A try, catch block lets you run code and have code that is run in the event of an exception (but not otherwise). We’ll print the exception message to the screen and then try again with our try, catch block. Modify your code to look like this:

static void Main(string[] args)
{
    try
    {
        int number1;
        int number2;
 
        Console.WriteLine("Enter a number, then hit enter:");
        number1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter another number, then hit enter:");
        number2 = int.Parse(Console.ReadLine());
 
        int sum = number1 + number2;
        Console.WriteLine("The sum is: " + sum.ToString());
 
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Main(args);
    }
}

Just like a method, a try, catch block uses curly brackets ( { and } ) to signify where the associated code goes. One of the wonderful things about C# is how the curly brackets let you format your code the exact way you want it, as opposed to Python which forces you to adhere to its own indentation requirements. Anyway, The code in the try block remains unchanged and if there are no errors, users will not know the difference from before. If, however, there is an exception, the user will see the message (which is a property of System.Exception – more on this in another lesson) and then get another shot at entering numbers, because the method Main calls itself, which causes the program to start over.

Image of exception handling

That’s way better than crashing.

Be careful when you instruct a method to call itself. If there is no way to exit the method (in this case, if the user successfully adds two numbers then hits enter), you will end up in an infinite loop. That’s fine in a simple application like this, but more complicated applications could crash.

I know you have one last question – why are we catching an Exception when Visual C# clearly told us that we were dealing with a FormatException? C# is strongly typed so we should have to catch what we throw? This is like catching a soccer ball with a baseball glove, right? Not exactly. It’s closer to catching a baseball with yellow stitches with a baseball glove.

FormatExceptions (and every other exception you will ever handle) are Exceptions because of something called inheritance (remember, I put a link at the top of this post). If you catch Exception ex, then you are catching every conceivable exception that could ever be thrown in that block of code. So, if you bother to watch for exceptions, why not take care of all of them?

That’s it for this installment. If you want to go a little farther, change this application to multiply three numbers together. Thanks for reading!

Creating a Calculator with C# Part 1: Installing Visual C# Express and Hello World

I think it’s wrong that I consider myself a C# developer and yet I have almost no C# code on my blog. I’ve set out to change that. Quite some time ago, my first real VB.NET program was a calculator. It wasn’t very functional, but I thought it was really cool and used it every once in a while. However, I now believe C# promotes much better coding practices than VB, so I’ll be using that.

This is a step-by-step, beginner-level series about C#. By the end, you’ll have a basic, fully-functional calculator with a Windows GUI. You’ll learn how to use the Visual Studio Windows Forms Designer. You’ll also learn a bunch of things about C#, including:

  • How to declare variables
  • Basic data types, including “string”, “int” and “double”
  • How to do basic math
  • How to concatenate strings
  • How to get user input from the console in console applications and from buttons in Windows applications
  • What events are and how to use them
  • How to do basic math
  • The basic syntax

You’ll also learn how to install Visual C# Express Edition. Oh, by the way, unless you have Visual Studio (which you probably don’t want to pay several hundred dollars for right now), you’ll need to download it. You can do so here. Just click the button that says Install Now. If English isn’t your first language, there’s an area on the middle-right of the page where you can choose a different one. If you can’t speak English, I don’t know how you made it this far, but I am really impressed.

Visual C# Installer Download Site

The install button is circled in red, just in case!

After you download the installer, go ahead and run it. You’ll see a window that looks a lot like this:

Installer window image

I assure you, C# is almost this easy once you get used to it.

You can uncheck the button that sends setup experiences to Microsoft, but since you’re getting an amazing application free-of-charge, you probably owe it to Microsoft to give them a little bit of information. When I first discovered Visual Studio Express, I thought it was a pirated version because it was such a good deal. Don’t worry, it’s legal. Hit the next button when you’re mentally prepared.

Installer window image

You might actually want to read through some of the license terms.

Tell Microsoft you read and agree to the license terms (I wonder if they suspect you’re lying…), then hit next.

Third installer slide image

You won’t need SQL Server or anything else that shows up here for a while, but if you want to start the download for when you do, I don’t blame you. It’s really big.

When you’ve decided what you want here (which doesn’t need to be anything), hit next.

Install Confirmation Slide

You’re almost there. I know it’s been hard, but I know you have it in you.

This is the confirmation slide of the installer. If everything looks good to you, hit the install button. Congratulations, your programming career has begun.

Installing Image

RMC has good Internet so this didn’t take me very long.

Let the installer do its thing, then when it’s done, open up Visual C#. Unfortunately, it wasn’t playing nice, so I’ll be doing my coding in the full version of Visual Studio. Don’t worry, they’re virtually the same thing and I’m looking up what to do on the menus on MSDN, which is the go-to resource for .NET developers like myself. By the way, the best way to search MSDN is with Google, especially since you’ll get results from other great resources like CodeProject and StackOverflow.

When you open up Visual C#, go to File > New Project. Select Console Application, and name it Hello World. Click OK. You’ll get a window that has this code in it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

What is all that junk? It will make more sense to you later, but here’s a general idea:

  • A namespace is the category that a class fits into. For example, the “Graphics” class is inside the System.Drawing namespace because it has to do with drawing. Namespaces can be inside of other namespaces. For example, the “Forms” namespace is inside the “System.Windows” namespace. The namespace in here is called Hello_World.
  • A class contains a series of methods, properties, variables, and events. Classes are the lowest level categorization in C#. Classes do something – for example, you can display a window on the screen with the Form class, and the Console class lets you read from and write to the console. You’ll be using the latter of the two later on. In this code, can you figure out what the class is? It’s Program. All variables have an associated class, but some classes can’t be created as variables for various reasons.
  • A method contains actual code that runs. You can variables, invoke (run or call) other methods, modify variables, do math, print, simulate a nuclear explosion, run a game, or crash Windows from within a method. Methods can take “arguments”, which are pieces of information a method needs to run properly. The method in the above code, Main, takes a string array called args. This is passed to the method by Windows; we don’t need to worry about it right now!
  • The using statements at the top of the page just allow us to access classes without typing out their namespace. If we wanted to write to the console, we would have to type out System.Console instead of just Console without the using System statement. The huge amount of convenience this provides will become evident pretty soon.

Now, let’s code our hello world application. Change your main method to look like this:

static void Main(string[] args)
{
    Console.WriteLine("Hello, world!");
    Console.ReadLine();
}

Two method calls have been added to our Main method. The first one is to Console.WriteLine(). Console.WriteLine takes what is called a string as an argument. A string is between two quotation marks and is just text. The string we provide will be written to the console screen. If we had more then one argument, we would separate them with commas.

The second call is to Console.ReadLine(). This forces our program not to exit until the user hits enter, so you can actually see your first message as a programmer. It’s pretty special – I still remember my hello world! The empty parentheses mean we are invoking the method Console.ReadLine but aren’t passing any arguments, since it doesn’t need them. Go ahead and run your project by hitting F5.

I’m not kidding when I say this is special. Save this project and show it to your grandchildren. If you already have them, show them anyway.

I know this tutorial went from very basic to rather complex (if you have never coded before) in a hurry. Don’t worry if you’re still fuzzy on what namespaces, classes and methods are. If you do, you’re in awesome shape! Still, just be happy that you have a working program. Congratulations, new developer!