Robert Bak – Notes

Some programming tips – updated sometimes

Basic socket server in AIR

The socket api is relatively simple, but I couldn’t find a simple step by step tutorial on getting sockets working. The basic idea was to create an app that can send text to connected clients (which I’ll be using to send code while teaching a Flex class), and getting that basic functionality working is amazingly simple with AIR 2+.

The config

To make life easier, first I’ve created a class that will hold the configuration for the socket, it’s nothing interesting, but as it will be used later, I’ll put it at the top. There are a few things to keep in mind while choosing the port – it needs to be smaller than 65536 and should be bigger than 1024 (see docs) and ports can’t be shared, so you need to make sure that the port you’re choosing isn’t already used 1.

public class SocketConfig
   static public const port:int = 9231;
   static public const address:String = "";

The server – setup

Creating the server is really easy. We just need to set up an instance of the ServerSocket class, and start listening and handling incoming connections.

// create a server socket
protected var socket:ServerSocket = new ServerSocket();

protected function createServer(event:FlexEvent):void
   // bind the socket to a given address and port
   // the CONNECT event is dispatched after a client connects
   // to the socket, make sure we handle it
   // start listening for connections

As it’s supposed to push data to multiple clients, here’s the code to keep track of the connections,

[Bindable] // for keeping track of the clients
protected var clientSockets:ArrayCollection = new ArrayCollection();

// adds the client to the list and adds the disconnect handler         
function clientConnectedHandler(event:ServerSocketConnectEvent):void{
function clientDisconnectedHandler(event:Event):void{

The client – setup

Setting up a client is as easy, if not easier. You just need to create a Socket, add event listeners to link some logic to it and connect to the server (using it’s address and port)

// create our client socket
protected var socket:Socket = new Socket();

protected function createSocket(event:Event):void
   // get notified when the socket connects
   // get notified when there's data
   // connect

We can create functions with trace statements to check if the connection works and, as the only thing this server will be sending will be strings, output the data is being sent.

protected function socketConnected(e:Event):void{
   trace("client - socket connected");
protected function socketData(e:ProgressEvent):void{
   trace("client - socket data");
   // read the string from the socket

The server – finishing

The last thing we need to do is send the data to sockets, we’re going to use the writeUTF 2 of the socket. Notice that this function is called on each of the connected sockets (stored in the clientSockets collection) and not on our ServerSocket instance. It’s also important to flush the data to the socket once finished to make sure the data is sent.

protected function writeDataToSockets(event:Event):void
   for each (var sock:Socket in clientSockets){

That’s it, now we have a fully working server which can push data to clients over TCP.

Please remember that this is a very basic example with no code for error handling, etc. Just enough code to get it working. The very last thing to do is make sure we release the port once the server application closes.

protected function applicationCloseHandler(event:Event):void

The source code has some additional code for the UI.
Download source code.


  1. Remember to close the socket when closing the app, otherwise you might run into trouble when launching it again
  2. writeUTF has a limit on string length – it needs to be shorter than 65536

8 thoughts on “Basic socket server in AIR

  • mark says:

    How to save socket.readUTF() to a local string

    I try to use s:String = socket.readUTF();

    I get #2030 error.Thanks


    • Robert Bak says:

      Did you remove the trace(socket.readUTF())? Reading from a socket removes what’s been read, and as error 2030 means that the “End of file was encountered.” (see the reference) I would guess you’re trying to read an empty socket.

  • Miguel says:


  • Cyril says:

    the link to download the source code doesn’t work…Could you fix it please ?

  • Jay says:

    Hello, I stumbled on this page and since the link for the source code is no longer active, I want to know if the server and the client are two different projects on flex?

  • Ahmed says:

    Thanks for the post.
    I have been working on a project for a while and came across with a problem. The server socket fails to send data to clients for some reason. Only one client gets the data. I fixed the issue by delaying the send (0.3 secs) and it works fine, though when there are 10 clients, the last clients gets the data after 3 seconds. 30 clients would mean 9 sec for the last to get the data.
    This works,but clearly, not the best way.
    I can also queue each data requested, in server in an array and send one by one.
    Still I must get rid of the 0.3 sec delay. Can you advise on this?

Leave a Reply

Your email address will not be published. Required fields are marked *