Hi All,
after long time I’m back for all the developers are working with the Flash Platform right now!
Sorry for that but it was a really intensive period for me with the organization of “Having fun with Adobe AIR” so I haven’t a lot of time to share with you my new experiments.
Yesterday Adobe MAX is finished with a lots of design news, great and inspire case histories for designers and a lot of amusement during the Sneak Peek where they have shown the real power of Adobe labs with tons of really cool features that we could see in next releases of Adobe’s softwares.
For a developer perspective there weren’t big announces so, as usual, we can do it by ourselves…. and here we are!
During last few days Adobe release the Adobe AIR 3.8 and Flash Player 11.8, both in BETA but you can download and start to play with them.
When Adobe releases a new AIR SDK I always take a look to the release notes to see the new features of my favorite platform, this time I’m glad to announce that they add the opportunity to create TCP/IP and UDP socket server directly on iOS and Android.
This is a very cool feature because you can really create amazing things in particular for applications and games, for example local multiplayer, chat and so on.
I worked a lots with sockets during last years in several projects and my big concern was that I can’t create a socket server on smartphone and tablet with AIR, I could do that only with native code but I was pretty sure to see this feature will be implemented in next releases of AIR!
Today I had few time to spend experimenting new stuff so I decided to try AIR 3.8 BETA on mobile and create something cool to share with you.
As you can see in this short video I create a socket server on my iPhone 4 that interact with a client made on my iPad mini (I tried also with my Android smartphone and it works as well):
To create this sample you needn’t to learn something new, you can use the same APIs you will use on a desktop application, so to create a socket server you write those few lines of code:
//creation of a TCP/IP Socket server
private function createServer():void{
server = new ServerSocket();
server.addEventListener(Event.CONNECT, onConnect);
server.bind(7934); // this is the number of the port where your socket communicate
server.listen();
}
Then, when a client socket will join in the same network and it listens the same port of the server, the magic happens and you can start to comunicate:
//on the server socket application protected function onConnect(event:ServerSocketConnectEvent):void { incomingSocket = event.socket; incomingSocket.addEventListener(ProgressEvent.SOCKET_DATA, onData);} protected function onData(event:ProgressEvent):void { if (incomingSocket.bytesAvailable > 0){ //here you can pass data to the client using writeBytes, writeUTFBytes and many other methods /*an example could be: incomingSocket.writeUTFBytes(String("HELLO!"); incomingSocket.flush();*/ } } // on the client socket application you have to create the connection and then manage (send and receive) data from the server private function createSocketConnection():void{ socket = new Socket() socket.addEventListener(Event.CONNECT, connectedToServer); socket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData); socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); socket.addEventListener(Event.CLOSE, closeSocket}); //pass to connect method the server IP and the port to comunicate socket.connect("127.0.0.1", 7934); } protected function receiveData(event:ProgressEvent):void { // here you can read all the packets sent from the server } protected function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } private function connectedToServer(e:Event):void{ //yes! you are connected to the socket server } private function closeSocket(e:Event):void{ //your socket connection is closed }
After that you can start to experiments with this new feature as I’ve just done.
Last but not least, as you can see on the release notes, Adobe adds another great feature, that is the capability to stop all movieclips are running on the stage calling a new method “stopAllChildren()” directly from the stage instance.
Simple, easy and useful!
very cool. Nice to see Adobe is still investing in the Air runtime.
Remarkable! Its actually remarkable article,
I have got much clear idea regarding from this post.
Thanks for sharing, this is surely fun stuff!
127.0.0.1? is two devices link to the same wifi? if i use 3G ,how do i set the ip address correct?
the devices must be in the same network.
you have to reach the IP of device where the server application is running.
In my example I put a generic IP but you always have to see where the server app works
so how can i know the exact server side ip address number if i use 3G?
or it must be in the same network? if server is far away from client ,any chance can connect ? ….thx for u reply , i have used these api using two air in win 7 , and i can find the ip number in win 7 system if they arent running in the same pc (using localhost) but i dont know how to find the ip address if i using mobile if the ip shouldnt be “localhost”…
check the settings of your device there you will usually see the IP address
Thank-you very much for your explanation and example code of this new feature.
Would you be able to explain how this is different than using RTMFP to create P2P connections on the same network?
Mainly with P2P all peers share the content to other peers.
In this case is the server application to manage anything and deliver anything.
Hi,
I would like to see the possibility sending packets to multicast IPs – is it possible in AIR 3.8?
Did you try to read this post: http://www.fabiobiondi.com/blog/2010/11/p2p-share-real-time-data-across-android-and-desktop-air-applications-to-build-multiuser-games-and-collaborative-ria/
Possible to Post the Sourcecode for the above Proof? I have sum problems to get a similar Proof running and at the Adobe Site don’t have any real good Sourcecodes Helpfiles. Thanx a bunch
I did it! Thank you for the tutorial, I have tried in Android and works perfectly. I have a question about ports. Which range of ports is recommend for each device. IOS or Android? I noticed that the script could give errors depending on the number of ports.
from documentation (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/ServerSocket.html):
Permission to connect to a port number below 1024 is subject to the system security policy. On Mac and Linux systems, for example, the application must be running with root privileges to connect to ports below 1024.
Ok … Got this working. Though i asked myself, when I start a server on different devices in WIFI (they always have different IPs), is it possible with AIR to look for the Server via Clientside inside the WIFI, to get the IP and then to connect automatically to the server, or do i always have to know the IP? I cant find anything, which brings a bit light into this.
You can create a native extension to do that
hello ppl,could any one plz send me the source code of this example.
Thanks in advance.
ServerSocketConnectEvent is not firing …. please help
probably your devices aren’t in the same network or your router doesn’t allow this thing.