Reputation: 5554
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?
Upvotes: 1
Views: 2044
Reputation: 3565
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
Upvotes: 2