Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Discover the UDP API in Adobe AIR 2

0 (0 Likes / 0 Dislikes)
[ringing sound] [ADOBE DEVELOPER CONNECTION] [Michael ChaIze - Platform Evangelist] Hi. I'm Michael Chaize, platform evangelist for Adobe. Today we will discover the new UDP API introduced in AIR 2.0, and we will enable communication between AIR and a Java server. But first, what is UDP? UDP means universal datagram protocol, and usually, Web applications prefer TCP to send data between a client and a server because, thanks to TCP, you can make sure that the data will be received and in the correct order. And this is not the case with UDP. But with TCP there is a lot of additional work to guarantee that you will receive the data. That's why sometimes you will prefer UDP when you want just a straightforward communication between the client and the server. And this is used mainly for video chat, audio chat, what we call voice over IP applications. They all use UDP because you just send the data without expecting anything, and it will consume less CPU on the server side. Also, you can use UDP for multiplayer games and real-time collaborative applications serving the enterprise world where data is not mission critical. And this is a fresh new API, so let's have a look at this very simple application where I use this new API. So here I am within Flash Builder, and as you can see, there is this new kind of object called DatagramSocket. If you look at the place of the subject, this starts within So this is a new class in a classic package, and I just create a new DatagramSocket object. Then once the application is created, you can add some event listeners. This is quite powerful thanks to Flex because you can add an event listener that an event will be fired if you receive some data coming from the server using ADP and also if you get a new error message or a security error message. That might happen because with UDP you need to take care of the domain names to enable communication. But today I will work on my local computer, so this is very easy. You can use it as a sample, and there will be no restriction. So the first thing to do is to bind your DatagramSocket to a dedicated port. So here I used a 4456 port, but you can use whatever port. This is using my local IP. And then if you switch to the design view, we can see that there is just a text import file, a button to send packets, and here I will display the response coming from the server. So if I click on the button, I will create a new packet to transmit some data. You can use a byte array, so you can send whatever you want. You can send string data, you can also send sound, video. If it's binary data, it will work. And then you just create a packet. So here I create a string data stream using the write UTF byte method. And I will send the packet on this dedicated port. Then I just declare here the receive methods. So it means that if a message using this port and this DatagramSocket comes from the server, then I want to get the message and handle it in this dedicated function, which is DataHandler, and I will just display on the user interface the message coming from the server. So this is it. And now if you switch to the Java code, you will see that it's kind of funny because it really looks like our Flex code, even the package. So here the DatagramSocket package is in, so it really looks like the Flash API. And here is my constructor and the main methods. So here this is the same. I create a DatagramSocket object. I will work on the same port. With Java, it's not as sophisticated as Flex, so basically, I need to loop and to listen at some events, and if I get something, then I will exit to the loop. We don't have this Add Event Listener method as in Flex, but it will work. You will see. Then I have what we call an Incoming Packet, so this will come from my Adobe AIR application. I will just print the results in the Java Console so that we can see what is transmitted. If I've got this message, so if AIR sends, "Hi, I'm an AIR2 application. Who are you?" then I will answer back. So to answer back with Java, I just need to create a string to encode the string using UTF-8 and then send it back. So to do so, I can get the IP address of the AIR application, and I can also get the port, and I fire this message back to the AIR application. So now if I run my Java application and then let me switch back to Flash Builder to run my AIR application, I can start the communication using UDP. So I can say, for instance, "Hello." And as you can see in the Java Console, I've got the message. And now if I write the expected sentence, which is, "Hi, I'm an AIR2 application. Who are you?" then I've got the answer, "I'm a pure Java software man. Nice to meet you." And it closes the Java software. So I encourage you to discover this new UDP API introducing AIR 2.0, and you can find the source code of this sample on my blog at [Michael Chaize's Blog -] Thanks. [ADOBE DEVELOPER CONNECTION]

Video Details

Duration: 7 minutes and 17 seconds
Language: English
License: All rights reserved
Genre: None
Views: 278
Posted by: adobetv on Oct 21, 2010

Platform Evangelist Michael Chaize explains the seamless data transmission capabilities of Adobe AIR 2 with Java clients, enabled by universal datagram protocol, or UDP.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.