midhunhk
midhunhk

Reputation: 5554

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();
        }

Upvotes: 0

Views: 1282

Answers (2)

Howard Roark
Howard Roark

Reputation: 301

This is an old question now, but since, on the ActionScript side you are using

socket.writeUTFBytes("HelloSocket");

On the Java side, change it to this and it will work without AMF :

BufferedReader in = new BufferedReader (new InputStreamReader((clientSocket.getInputStream())));
String line = "";               
while( (line = in.readLine()) != null) {
    processMessage(line);
}

Upvotes: 1

Dennis Jaamann
Dennis Jaamann

Reputation: 3565

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

Upvotes: 2

Related Questions