Remoting into a Raspberry Pi

by david 9. June 2012 23:40

The little LCD TV I tested the Raspberry Pi with wasn't very good, so I wondered if it was possible to remote into the Raspberry Pi from another machine. I found that this is no problem when I looked here. So I tried the following commands:

sudo apt-get install tightvncserver
tightvncserver

...on the Raspberry Pi and it worked perfectly. Then with a copy of the TightVNC client running on my windows machine, I can access the Raspberry Pi remotely. This is awesome.

I used that approach to do more testing of my simple C# webserver, so here I am testing the webserver in TightVNC ... and also checking that the page is being served over my network to the PC in internet explorer:

...and finally, here is the Raspberry Pi serving the same test page to my iPhone:

OK, that all seems to work. I've just got to figure out what to do next now.

Unexpected Pi

by david 8. June 2012 22:13

A very kind colleague of mine has lent me his Raspberry Pi, so that I can have a little play with it. Here it is being hooked up on my workbench:

I used the standard Debian 'squeeze' image, since that's what I've been messing about with recently. After checking that I had internet access, I went off and installed the mono runtime, like this:

sudo apt-get update
sudo apt-get install mono-runtime

...after doing that I ran the C# webserver example that I had already written - and it works a treat! I didn't compile the code on the Raspberry Pi, I simply copied the exe file over and ran it with the mono command. The code was compiled in monodevelop running on Debian in VirtualBox.

Anyway, here is another photo, taken when I was first cheking that I could get online. Umm, I need to tidy up in here again.

More reports will follow I'm sure...

Raspberry Pi Preparations

by david 29. May 2012 22:46

I am currently in the queue to receive a Raspberry Pi. Apparently my machine is about 5 weeks away. So I have decided that I should begin making some preparations. I have downloaded an ISO image of Debian "squeeze" (the version with the LXDE desktop). So at least I should know my way round the OS in advance.

After installing the Debian ISO on a virtual machine, the first thing that I did was install the monodevelop IDE which I don't think will actually work on the Raspberry Pi ... but should enable me to try some things out and then just compile them from the command line on the Pi (hopefully).

Monodevelop seemed to work like a charm on Debian, so I grabbed my C# webserver example to try out some code.

Amazingly, the only real thing that I had to change was add the following delegate declaration:

public delegate TResult Func<in T1, out TResult>(T1 arg1);

...apart from that the code worked without any changes. Here is a screenshot of it all working:

So I am hopeful that I will be able to run my little slimline webserver on my Raspberry Pi when it arrives.

Simple C# Web Server

by david 1. April 2012 17:28

During my lunchbreak the other day I wondered how hard it would be to write a simple webserver in C#. Surely with all the toys in the .net framework these days it should be pretty easy? So I decided to give it a go and see what happens. The aim was to write a simple class that recieves a delegate to a method which is responsible for the page content, everything else could be taken care of by the class I'm writing. That way the server can serve whatever page(s) you like, but you just need to write a simple method that returns a string with some html in it. Of course, with this approach the HttpListener is our friend and does much of the hard stuff. Here it is in action running on my Iconia tablet:

The webserver running

It took me about 30 minutes to get it working. After taking the code home and tidying it up a bit, this is what now I have:

using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
 
namespace SimpleWebServer
{
    public class WebServer
    {
        private readonly HttpListener _listener = new HttpListener();
        private readonly Func<HttpListenerRequeststring> _responderMethod;
 
        public WebServer(string[] prefixes, Func<HttpListenerRequeststring> method)
        {
            if (!HttpListener.IsSupported)
                throw new NotSupportedException(
                    "Needs Windows XP SP2, Server 2003 or later.");
 
            // URI prefixes are required, for example 
            // "http://localhost:8080/index/".
            if (prefixes == null || prefixes.Length == 0)
                throw new ArgumentException("prefixes");
 
            // A responder method is required
            if (method == null)
                throw new ArgumentException("method");
 
            foreach (string s in prefixes)
                _listener.Prefixes.Add(s);
 
            _responderMethod = method;
            _listener.Start();
        }
 
        public WebServer(Func<HttpListenerRequeststring> method, params string[] prefixes)
            : this(prefixes, method) { }
 
        public void Run()
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                Console.WriteLine("Webserver running...");
                try
                {
                    while (_listener.IsListening)
                    {
                        ThreadPool.QueueUserWorkItem((c) =>
                        {
                            var ctx = c as HttpListenerContext;
                            try
                            {
                                string rstr = _responderMethod(ctx.Request);
                                byte[] buf = Encoding.UTF8.GetBytes(rstr);
                                ctx.Response.ContentLength64 = buf.Length;
                                ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                            }
                            catch { } // suppress any exceptions
                            finally
                            {
                                // always close the stream
                                ctx.Response.OutputStream.Close();
                            }
                        }, _listener.GetContext());
                    }
                }
                catch { } // suppress any exceptions
            });
        }
 
        public void Stop()
        {
            _listener.Stop();
            _listener.Close();
        }
    }
}

All the work is done on background threads, which will be automatically cleaned up when the program quits.  Example use of this code is pretty simple, like this:

class Program
{
    static void Main(string[] args)
    {
        WebServer ws = new WebServer(SendResponse, "http://localhost:8080/test/");
        ws.Run();
        Console.WriteLine("A simple webserver. Press a key to quit.");
        Console.ReadKey();
        ws.Stop();
    }
 
    public static string SendResponse(HttpListenerRequest request)
    {
        return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>"DateTime.Now);    
    }
}

I'm even considering using this code to build a little DALIS aware test server, so that I can run DALIS programs without hosting anything in IIS... even better I could run a couple of them and bounce DALIS programs between them.


EDIT:This little bit of code has proved quite popular, and recently I have been asked if it has been released under any particular license. Since it was such a small bit of code and just an evolution of the MSDN documentation found here, I didn't really think about a license for it. But I am happy to share this code, so to make the situation clear I'm releasing it under the MIT License.


PDP-11 webserver

by david 1. April 2011 22:39

I thought that the simplest application for my PDP-11 / WiFly combo would be to serve up some basic web pages.  So I've been tinkering around with some C code to make that work.  The HTTP protocol is not too hard once you get those CRLFs in the right places...  So here is a screenshot of my PDP-11 serving it's first webpage to my iPhone:

PDP serving webpage to iPhone

I'm pretty happy with that.  For the moment I'll ignore the fact that when you exit the webserver program it crashes RT-11.  I've probably made some stupid typo.  But whilst the program is running, it seems to work OK.  SMTP is next on my list, I want to send mail from my PDP.

About the author

David

I'm a C# developer having worked with .Net since it was in beta.  Before that I mainly worked in C and C++.  I have been developing commercial software for more than 20 years.  I also mess around with microprocesors, but that's just for fun.  I live near Cambridge, England and work for one of the departments at Cambridge University.

Tag cloud