Search code examples
javaactionscript-3apache-flexsocketsblazeds

How to send data in AMF format from flex to java socket server?


i want to know how to send data using the AMF format from my flex AIR project to a socket written in Java. I am getting CorruptedStreamException when sending data using writeUTFBytes() methods. Has anyone experienced similar problems? Also can AMF be used only if i am using LCDS only?

private SimpleServer(int port)
{
    System.out.println(">> Starting SimpleServer on port " + port);
    try
    {
        socket      = new ServerSocket(port);
        incoming    = socket.accept();
        objectInputStream   = new ObjectInputStream(incoming.getInputStream());
        objectOutputStream = new ObjectOutputStream(incoming.getOutputStream());
        boolean done = false;
        while (!done)
        {
            Object obj = objectInputStream.readObject();
            System.out.println( obj.toString() );
            if(obj == null)
            {
                done = true;
                incoming.close();
            }
        }
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

And my as3 function to send data to the server is

        private function onSendClick():void
        {
            var host:String = "10.87.118.8";
            var port:int = 9090;

            var socket:Socket = new Socket();

            trace("Connect");
            socket.connect(host, port);

            trace("write");
            socket.writeUTFBytes("HelloSocket");

            trace("flush");
            socket.flush();
        }

Solution

  • AMF stands for Action Message Format.

    It is a specification which defines how to transfer data between an ActionScript client and external system.

    Therefore, many server side technologies incorporate AMF into their packages. For example BlazeDS, GraniteDS, pyAMF, amfphp, ...

    Hence, to answer your question, no AMF can also be used outside of LCDS. It is merely an "envelope" you can use to send your message (=data) in.

    It should even work with sockets. I believe there is an open source library called merapi that uses this principle.

    Cheers