Archive for November, 2009

AIR 2.0 Webserver – using ServerSocket API

Tuesday, November 17th, 2009

Inspired by (and based on) Christian Cantrells httpeek AIR application, I’ve knocked up a quick proof of concept http server in AIR 2.0 Beta.
It’s basic (understands GET and HEAD requests only), largely untested and almost certainly has bugs, but in a “Works on my machine” basis it does seem to work.
What you should definitely NOT do is to expose it to the outside world – I can’t say it’s not exploitable to read or execute files on your machine.

So it’s really just for interest only.

I’m struggling to find any compelling reason why you would actually want to use such a thing, to be honest.
Firstly, it only has one thread to work with, so performance is always going to be an issue when serving more than a very few clients.
If you wanted to have something a bit more sprightly or fully featured then it might be a better idea to start up a dedicated server program via the new nativeProcess API.

I did consider that it could be of (limited) use in either a classroom or home network scenario.
In a classroom it could perhaps serve course materials to accompany a presentation on a projecter, but realistically most lecturers are going to struggle to pick a network interface and deal with firewall issues in order to set it up.
Perhaps in a home network scenario one could use it to serve up a remote control interface to something like an mp3 player.
However, you would have to implement something like flash remoting or connect back to the server on another socket to establish control.

Bright ideas are welcome, as are bug reports (but be aware that unless I find a good reason to do so I’m unlikely to take this any further…)

Source code

Usage : Pick a network IP address (Normally 127.0.0.1 for local testing, if you pick one exposed to the network then be aware that it may well be firewalled – depending on your machines setup)
Pick a port number to serve on (if it’s already in use then you’ll get an error, note that ports below 1024 normally require admin rights to serve on unix machines).
Choose a directory from which to serve files and click the “Serve” button – then point your browser to http://[yourIp]:[yourPortNo]/[yourFile].
Note that this does not serve directory listings – you’ll need to point it to an actual file e.g. “index.html” or whatever you’re using.