HTTP server with AIR 2.0

almost 2 years ago malczak actionscript comments

This time just a short note about new AIR 2.0 release candidate, available at Adobe Labs. I have written an example application to illustrate how to use new ServerSocket class in AIR applications. I have used that class to create simple HTTP server. In AIR 2.0 we have a new class available - ServerSocket. This class allows us (developers) to create TCP servers in our AIR applications. To setup server, all we have to do is to define local port and address. In my example application I'm using port 8888. Local address is defined based on available network interfaces. I'm using NetworkInterface (this is also an AIR 2.0 new class) to get first of active network interfaces. Application then uses IP address assigned to selected interface to run server. Thanks to this, if Your computer has network connection, application will use your ethernet IP, in other case it should use loopback interface. Below you can find code for initializing server.

 1 //create server instance
 2 srv = new ServerSocket();
 3 srv.addEventListener(Event.CONNECT, srvconnect );
 4 
 5 //search for active network interface
 6 var aaddr:InterfaceAddress;
 7 var ifaces:Vector.<networkinterface> = NetworkInfo.networkInfo.findInterfaces();
 8 if ( ifaces &amp;&amp; ifaces.length ) {
 9     var iface:NetworkInterface;
10     var addr:InterfaceAddress;
11         for each ( iface in ifaces )
12             if ( iface.active )
13                 for each ( addr in iface.addresses ) {
14                     //select interface, but dont stop searching
15                     //we will use last one 
16                         if ( iface.active &amp;&amp; addr.ipVersion==IPVersion.IPV4 )
17                             aaddr = addr; 
18                 };
19 } else log("network interfaces error !");
20 
21 try {
22     //bind to port on selected address or use loopback
23     srv.bind( 8888, (aaddr)?aaddr.address:"127.0.0.1" );
24     
25 } catch ( e:Error ) {
26      Alert.show("Unable to open port", "Error", Alert.OK );
27      return;
28 }
29 
30 //initialize listening for TCP connections 
31 srv.listen(1);
32 </networkinterface>

Application can recognize only few, most basic, HTTP requests, just to illustrate how you can write servers in AS3 with AIR 2.0. Running application is shown below. You can also see a servers default page displayed in Safari browser. running http server

And here comes most important part... source code

Download HTTP server source code in Flash Builder 4 project package format - HERE

If you are not a Flash Builder user, you can download zipped sources below...

Download HTTP server zipped source - HERE

You should also download AIR 2.0 RC SDK from Adobe Labs

Comments

almost 2 years ago edward #0

how does one open an FXP file, my mac CS4 program has no knowledge of FXP.... this must be some packaged XML compressed format, not a ZIP?

almost 2 years ago malczak #1

Hi,
FXP is format for Flash Builder 4 projects. If You're using Flash Builder 4 just select 'File > Import' to import fxp into your workspace. But if You are not using Flash Builder 4, try to download source code in zip format.

New comment

  1. (with http or https prefix)

  2. (use [code][/code] tags to post a code snippet)

  3. (javascript is required)

Website content premeditately commited by malczak & sobstel.
Layout by mlando. Icons by dryicons.com. All rights reserved.