Pushing data to Flash using binary sockets
// December 17th, 2008 // Experiments, Source code & tutorials
My personal challenge for today was to create a Java socket server that pushes data to flash using binary sockets. That sounds harder than it is, to my surprise it was actually quite simple.
I never made something with Java before, but its very similar to ActionScript 3 (or the other way around ) and if you already use Eclipse for your ActionScript development, you will find it not hard to start with.
The ActionScript socket part is even easier:
Make a socket object, connect to your host an server port, and start listening to the incoming bytes
var s:Socket = new Socket("localhost",9999) s.addEventListener(ProgressEvent.SOCKET_DATA, getData) private function getData(event : ProgressEvent) : void { var output:String =s.readUTFBytes(s.bytesAvailable) trace(output) }
To send data to the server you just write the bytes an flush.
s.writeUTFBytes("Hello server") s.flush()
et Voila:

Download the Java and Flash source: flash_java_socket_src
(This is my first java “app”, don’t expect it to be great and free of bugs
)
















Hi, I ran into a tricky problem. I can see a message being sent from the server (in case you are interested, its a mina-based server). The client seems to be missing the message. Should a client be calling socket.readUTFBytes(socket.bytesAvailable) in a loop?
Thanks,
No, no loop, just the ProgressEvent.
Try adding these events for debugging: Event.CONNECT,IOErrorEvent.IO_ERROR, SecurityErrorEvent.SECURITY_ERROR
Also, now I’m implementing it in a real world example I noticed you have to sheck a policy file from a socket server on the same domain
Security.loadPolicyFile(“xmlsocket://localhost:9998)
Google for “Socket policy server” for an example.
Can you pls suggest me the C++ Code instead of Java server, I
I don’t know much about C++, but maybe this will help? http://www.alhem.net/Sockets/tutorial/