Requirements
Prerequisite knowledge

This article is intended for developers who
are comfortable with ActionScript 3 and
who have at least a basic understanding of
TCP sockets.
Required products

User level

Intermediate
 
This article discusses the new socket server support in AIR 2. The ability to listen on a socket means that your AIR 2 applications can send and receive data over a TCP socket without having to initiate the socket connection. Being able to accept incoming socket connections never made much sense for SWF content running in the browser, but in the context of desktop applications written for Adobe AIR, it opens up all kinds of new possibilities.
 
This article covers the following topics:
 

 
History of socket support in Flash Player

Flash Player has a long history of support for TCP sockets. The XMLSocket was first introduced in Flash Player 5. XMLSockets provide developers with an easy way to read and write XML data between a client and a server across a TCP connection. In Flash Player 9, after watching developers use the XMLSocket class to read and write all kinds of other data besides just XML, the Socket class was introduced.
 
The Socket class (along with ByteArrays) gives Flash developers true binary socket support, which enables them to pass data between clients and servers using protocols other than HTTP. When the Flash Platform was extended to the desktop with Adobe AIR, developers started asking for the ability to create socket endpoints so as to be able to listen on sockets. AIR 2 delivers this capability with the ServerSocket class.
 

 
Introducing the ServerSocket class

The ServerSocket class enables developers to accept incoming socket connections, as opposed to just being able to open connections from clients. In other words, using the ServerSocket class enables applications to actually become servers. Establishing a socket endpoint has several interesting use cases:
 
  • Inter-application communication: Applications can communicate with each other by listening on sockets, connecting to each other, and passing data back and forth.
  • Peer-to-peer applications: It's becoming increasingly useful for applications to be able to share data with each other across networks, as opposed to just with servers.
  • Additional protocol support: Protocols such as FTP may require clients to listen on specific ports so the server can connect back to the client (as in the case of active mode).
  • Network utilities: Applications like proxy servers require the use of both incoming and outgoing socket connections.
Creating a socket endpoint is as easy as creating an outgoing socket. The following code shows how to listen for incoming TCP connections:
 
import flash.net.ServerSocket; import flash.events.ServerSocketConnectEvent; private function onConnect(e:ServerSocketConnectEvent):void { var incomingSocket:Socket = e.socket; // You can now read and write data from the socket instance } var server:ServerSocket = new ServerSocket(); server.addEventListener(Event.CONNECT, onConnect); server.bind(8888); // Pass in the port number you want to listen on server.listen();

 
Tips on using the ServerSocket class

Keep in mind the following if you want your application to act as a server:
 
  • For security reasons, the ServerSocket class can only be used in the application sandbox.
  • Applications that attempt to bind to ports below 1024 will be subject to the operating system's security policy. On Mac OS X and Linux, these applications will need to run as root. For this reason, it's best for most applications to use nonprivileged ports.
  • The ServerSocket constructor takes a backlog argument. If not provided or set to 0 (zero), the backlog is the maximum value supported by the operating system.

 
HTTPeek: An AIR 2 HTTP proxy server

HTTPeek, shown in Figure 1, is a sample application I wrote to demonstrate the use of the ServerSocket class. It's an HTTP proxy which logs request and response headers of all HTTP traffic proxied through it, and is useful for debugging web applications. You can download the source code from Google Code or the AIR Developer Center.
 
The HTTPeek application
Figure 1. The HTTPeek application
 

 
Where to go from here

This article covered basic information about listening for incoming TCP sockets in Adobe AIR 2. For more information on using sockets in Flash Player, see: