Sending messages from server socket to client sockets

In this example i want to show you how to send messages from server socket(AIR app) to client sockets(AIR apps). You can install all 3 applications on the same computer or ServerSocketDemo.air on the server(it could be your mac or pc) and SocketDemo.air on any other computer on your local area network(LAN).

NOTE: You will need AIR 2 BETA – Download the Runtime

Files: ServerSocketDemo.air, SocketDemo.air, SocketDemo2.air
Source Codes: ServerSocketDemo.mxml, SocketDemo.mxml

11 Comments


  1. havarez
    February 22, 2010 at 8:15 am

    Hi Mariush!

    That is the cleanest and best example of socket connections to start from.
    It whould be very interesting if you make more studies.
    Many thanks!

  2. rudi
    March 9, 2010 at 10:24 pm

    hi!
    I am trying to play with server socket but I don’t realize how solve this:
    many clients send to server a lot of different requests, the server get this requests, find data from a db in asyncronous mode and respond to a proper client… how can the responder find the correct client???
    Sorry for my poor english, and phereps for the stupid request!
    But if you have same suggests… thanks!

  3. Mariush T.
    March 10, 2010 at 12:40 am

    Hi Rudi,
    Use AsyncToken and add any property name to it.
    var token:AsyncToken = myService.send();
    token.socket = event.target;

    When the result comes back, get not only the data from the ResultEvent but the token as well:
    var token:AsyncToken = resultEvent.token;
    var socket:Socket = token.socket

    AsyncServerSocketDemo.mxml

  4. rudi
    March 10, 2010 at 9:28 am

    Thanks! but in the Server the asyncronous operation is in local and made with an sqlite connection… I don’t have myService.send()
    How can I do?
    my server call this function:
    public function execute(sql:String, parameters:Object, handler:Function, itemClass:Class=null):void
    {
    sqlRunner.execute(sql, parameters, handler, itemClass);
    }
    but this function return void and not a token…
    I’am lost ;)

  5. Mariush T.
    March 10, 2010 at 2:15 pm

    You can use a Responder to handle results or register event listener to SQLStatement. Justin Palmer has great tutorial Dealing with Asynchronous Queries in Adobe AIR

  6. rudi
    March 10, 2010 at 5:53 pm

    thank you very much!!!

  7. Havarez
    March 18, 2010 at 4:19 pm

    Hi again!

    Sorry to bother but I spent so many time and nothing.

    I have 3 questions that I can’t solove:
    1)How to send Objects using socket methods – readObject() and writeObject()?
    2)When sending data from clien how to listen for responce to that data from server? (You can sed 10 requests and wait for responce on them)
    3)How can I encrypt password(or any data I want) when sending by socket connection?

    Thank you!

  8. Mariush T.
    March 18, 2010 at 6:32 pm

    1. Yes, you can use readObject() and writeObject() methods.
    2. I think i would use ‘Message concept’ from Multicore PureMVC framework with Pipes Utility. I would create and send custom Message(s) via socket. Look at the CustomPipeMessages example. Client sends new ContactMessage(ContactMessage.ADD_CONTACT, contact) and waits for ContactMessage(ContactMessage.CONTACT_ADDED), look at the handlePipeMessage() method in JunctionMediatos class.
    3. Google and you should be able to find open source AS3 libraries that you can use to deal with data encryption in Flex.
    Encryption in AS2 and AS3
    Easy Blowfish Text Encryption

  9. rudi
    May 2, 2010 at 9:16 am

    Hi!
    I have another big problem with socket!!!
    the client establishes a connection with the socket, the client asks the server information that may be heavy as images. when the data arrives at the client, the client uses a readObject, but it happens that data are not all arrived and generates a readObject EOFError … subsequently further reading does not allow to retrieve the object through the readObject … How can I fix this damn problem? you have some ideas or advice for me? through the event, I know the bytes available but I can not know the total bytes! how do I know when all data has arrived? Why readObject after the reading error no longer works? thanks for the help!

  10. rudi
    May 4, 2010 at 7:59 pm

    I solved.
    at least I hope.
    the problem was that the server forwards the response packet and the client did not expect, I thought that a response came in a single solution … not so
    In addition, the server can decide to meet more client requests made in the same package … and my client did not know!
    I also had to deal with this problem …

  11. Erik
    April 15, 2011 at 1:27 am

    Thanks for the muti-client example. I’m using Flash CS 5 and had to change serverSocket.bind( int(portInput.text), hostInput.text); to serverSocket.bind( int(portInput.text);

Leave a comment