1

I have a Flex AIR app, and i am trying to get data from a Java Server. Can some one tell me if this approach will work?

Start a ServerSocket in Java and wait for a connection.
Use Flex to connect to the same port.
Write some data to the socket from Java Server.
Read the data at the Flex end and process it.

-- Update : I think i am able to get this working, but the data that i read back at Flex end seems to be empty.

My server code is :

        socket  = new ServerSocket(port);
        client  = socket.accept();

        InetAddress address = client.getInetAddress();           

        BufferedReader in = new BufferedReader( new InputStreamReader(
                client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream());            

        out.println("hi");
        out.flush();

And my code in Flex is

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

            var socket:Socket = new Socket();
            socket.endian = Endian.BIG_ENDIAN;
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.connect(host, port);
        }

        private function onConnect(event:Event):void
        {
            trace(" Connected to server socket ");
            var socket:Socket = Socket(event.target);
            var obj:String = socket.readUTFBytes( socket.bytesAvailable);
            trace(obj);
        }

My current problem is socket.bytesAvailable becoming 0 and therefore obj is coming up as "". So how to read data sent from the server using a Socket in Flex?

7
  • 1
    Why don't you just use BlazeDS/LCDS functionality then? Are you using Flex in browser or are you using AIR. If you are using flex in browser, I fail to see the point why you would be writing a socket server yourself. Commented May 19, 2011 at 11:42
  • I am using AIR. And trying to connect to a ServerSocket opened by Java. Commented May 19, 2011 at 11:47
  • May I then suggest you have a look at the merapi source code at merapi.googlecode.com/svn/trunk You might be able to get the answers you need by looking at their code. Otherwise you could also just use merapi, it is free and it works just fine. You should also be aware that there are size limitations in terms of data in kb you can send through a socket from flex to java. Commented May 19, 2011 at 11:55
  • In my case the server will be writing data to the socket, i've seen numerous examples of other way around. Commented May 19, 2011 at 11:59
  • 1
    merapi goes both ways :P Commented May 19, 2011 at 12:04

1 Answer 1

2

After some basic googling, I've found a blog post that does exactly what you are describing: http://arthurnn.com/blog/2010/12/09/socket-connection-beteween-flex-air-and-java/

The example however runs the java app on the localhost, not remotely like you describe. Therefore it might occur that you will encounter some sandbox violations when trying to connect to the remote server socket. This might be solved by adding a crossdomain.xml to the root of java app server.

In fact, this blog post is a rudimentary example of what merapi does under the hood.

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

Seems to be just what i was looking for. Thanks a ton dennis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.