Basics of networking and communication

Flash Player 9 and later, Adobe AIR 1.0 and later

When you build applications in Flash Player or AIR, you often need to access resources outside your application. For example, you might send a request for an image to an Internet web server and get the image data in return. Or, you might send serialized objects back and forth over a socket connection with an application server. The Flash Player and AIR APIs provide several classes that allow your applications to participate in this exchange. These APIs support IP-based networking for protocols like UDP, TCP, HTTP, RTMP, and RTMFP.

The following classes can be used to send and receive data across a network:

Class

Supported data formats

Protocols

Description

Loader

SWF, PNG, JPEG, GIF

HTTP, HTTPS

Loads supported data types and converts the data into a display object.

See Loading display content dynamically.

URLLoader

Any (text, XML, binary, etc.)

HTTP, HTTPS

Loads arbitrary formats of data. Your application is responsible for interpreting the data.

See Using the URLLoader class

FileReference

Any

HTTP

Upload and download files.

NetConnection

Video, audio, ActionScript Message Format (AMF)

HTTP, HTTPS, RTMP, RTMFP

Connects to video, audio and remote object streams.

Sound

Audio

HTTP

Loads and plays supported audio formats.

XMLSocket

XML

TCP

Exchanges XML messages with an XMLSocket server.

See XML sockets .

Socket

Any

TCP

Connects to a TCP socket server.

See Binary client sockets .

SecureSocket (AIR)

Any

TCP with SSLv3 or TLSv1

Connects to a TCP socket server that requires SSL or TLS security.

See Secure client sockets (AIR) .

ServerSocket (AIR)

Any

TCP

Acts as a server for incoming TCP socket connections.

See Server sockets .

DatagramSocket (AIR)

Any

UDP

Sends and receives UDP packets.

See UDP sockets (AIR)

When your Flash Player or AIR application needs to communicate with another Flash Player or AIR application on the same computer, you can use the LocalConnection class. For example, two (or more) SWFs on the same web page can communicate with each other. Likewise, a SWF running on a web page can communicate with an AIR application. See Communicating with other Flash Player and AIR instances .

When you need to communicate with other, non-SWF processes on the local computer, you can use the NativeProcess class added in AIR 2. The NativeProcess class allows your AIR application to launch and communicate with other applications. See Communicating with native processes in AIR .

When you need information about the network environment of the computer on which an AIR application is running, you can use the following classes:

  • NetworkInfo—Provides information about the available network interfaces, such as the computer’s IP address. See Network interfaces .

  • DNSResolver—Allows you to look up DNS records. See Domain Name System (DNS) records .

  • ServiceMonitor—Allows you to monitor the availability of a server. See Service monitoring .

  • URLMonitor—Allows you to monitor the availability of a resource at a particular URL. See HTTP monitoring .

  • SocketMonitor and SecureSocketMonitor—Allows you to monitor the availability of a resource at a socket. See Socket monitoring .

Important concepts and terms

The following reference list contains important terms that you will encounter when programming networking and communications code:

External data
Data that is stored in some form outside of the application, and loaded into the application when needed. This data could be stored in a file that’s loaded directly, or stored in a database or other form that is retrieved by calling scripts or programs running on a server.

URL-encoded variables
The URL-encoded format provides a way to represent several variables (pairs of variable names and values) in a single string of text. Individual variables are written in the format name=value. Each variable (that is, each name-value pair) is separated by ampersand characters, like this: variable1=value1&variable2=value2. In this way, an indefinite number of variables can be sent as a single message.

MIME type
A standard code used to identify the type of a given file in Internet communication. Any given file type has a specific code that is used to identify it. When sending a file or message, a computer (such as a web server or a user’s Flash Player or AIR instance) will specify the type of file being sent.

HTTP
Hypertext Transfer Protocol—a standard format for delivering web pages and various other types of content that are sent over the Internet.

Request method
When an application (such as an AIR application or a web browser) sends a message (called an HTTP request) to a web server, any data being sent can be embedded in the request in one of two ways; these are the two request methods GET and POST. On the server end, the program receiving the request will need to look in the appropriate portion of the request to find the data, so the request method used to send data from your application should match the request method used to read that data on the server.

Socket connection
A persistent connection for communication between two computers.

Upload
To send a file to another computer.

Download
To retrieve a file from another computer.

Network interfaces

You can use the NetworkInfo object to discover the hardware and software network interfaces available to your application. The NetworkInfo object is a singleton object, you do not need to create one. Instead, use the static class property, networkInfo , to access the single NetworkInfo object. The NetworkInfo object also dispatches a networkChange event when one of the available interfaces change.

Call the findInterfaces() method to get a list of NetworkInterface objects. Each NetworkInterface object in the list describes one of the available interfaces. The NetworkInterface object provides such information as the IP address, hardware address, maximum transmission unit, and whether the interface is active.

The following code example traces the NetworkInterface properties of each interface on the client computer:

<html> 
<head> 
<script src="AIRAliases.js"></script> 
<script language="javascript"> 
function getNetInfo() 
{ 
    var networkInfo = air.NetworkI    nfo.ne tworkInfo; 
    var interfaces = networkInfo.findInterfaces(); 
     
    if( interfaces != null ) 
    { 
        air.trace( "Interface count: " + interfaces.length ); 
        for ( var i = 0; i < interfaces.length; i++ ) 
        { 
            var interfaceObj = interfaces[i];  
                   a ir. tr   a c  e( " \nna m e: "              + i nterf ac eOb j. nam e  ); 
             ai r.trace(  "display name: "     + interfaceObj.displayName ); 
            air.trace( "mtu: "                 + interfaceObj.mtu ); 
            air.trace( "active?: "             + interfaceObj.active ); 
            air.trace( "parent interface: " + interfaceObj.parent ); 
            air.trace( "hardware address: " + interfaceObj.hardwareAddress ); 
            if( interfaceObj.subInterfaces != null ) 
            { 
                air.trace( "# subinterfaces: " + interfaceObj.subInterfaces.length );             } 
            air.trace("# addresses: "     + interfaceObj.addresses.length ); 
            for( var j = 0; j < interfaceObj.addresses.length; j++  ) 
            { 
                var address = interfaceObj.addresses[j]; 
                air.trace( "  type: "           + address.ipVersion ); 
                air.trace( "  address: "         + address.address ); 
                air.trace( "  broadcast: "         + address.broadcast ); 
                air.trace( "  prefix length: "     + address.prefixLength ); 
            } 
        }             
    } 
} 
</script> 
</head> 
<body onload="getNetInfo()"> 
</body> 
</html>

For more information, see:

Network connectivity changes

Your AIR application can run in environments with uncertain and changing network connectivity. To help an application manage connections to online resources, Adobe AIR sends a network change event whenever a network connection becomes available or unavailable. Both the NetworkInfo object and the application’s NativeApplication object dispatch the networkChange event. To react to this event, add a listener:

air.NetworkInfo.networkInfo.addEventListener(air.Event.NETWORK_CHANGE, onNetworkChange);

And define an event handler function:

function onNetworkChange(event) 
{ 
    //Check resource availability 
}

The networkChange event does not indicate a change in all network activity, only that an individual network connection has changed. AIR does not attempt to interpret the meaning of the network change. A networked computer can have many real and virtual connections, so losing a connection does not necessarily mean losing a resource. On the other hand, new connections do not guarantee improved resource availability, either. Sometimes a new connection can even block access to resources previously available (for example, when connecting to a VPN).

In general, the only way for an application to determine whether it can connect to a remote resource is to try it. The service monitoring framework provides an event-based means of responding to changes in network connectivity to a specified host.

Note: The service monitoring framework detects whether a server responds acceptably to a request. A successful check does not guarantee full connectivity. Scalable web services often use caching and load-balancing appliances to redirect traffic to a cluster of web servers. In this situation, service providers only provide a partial diagnosis of network connectivity.

Service monitoring

Adobe® Flash® Builder includes this library automatically.

The service monitor framework functions separately from the AIR framework. For HTML-based AIR applications, the aircore.swf must be included in your AIR application package. The aircore.swf must also be included in your AIR application code, as follows:

<script src="aircore.swf" type="application/x-shockwave-flash"/>

The aircore.swf file is included in the libs\air directory of the AIR SDK.

The ServiceMonitor class implements the framework for monitoring network services and provides a base functionality for service monitors. By default, an instance of the ServiceMonitor class dispatches events regarding network connectivity. The ServiceMonitor object dispatches these events when the instance is created and whenever the runtime detects a network change. Additionally, you can set the pollInterval property of a ServiceMonitor instance to check connectivity at a specified interval in milliseconds, regardless of general network connectivity events. A ServiceMonitor object does not check network connectivity until the start() method is called.

The URLMonitor class, a subclass of the ServiceMonitor class, detects changes in HTTP connectivity for a specified URLRequest.

The SocketMonitor class, also a subclass of the ServiceMonitor class, detects changes in connectivity to a specified host at a specified port.

Note: Prior to AIR 2, the service monitor framework was published in the servicemonitor.swc library. This library is now deprecated. Use the aircore.swc library instead.

Flash CS4 and CS5 Professional

To use these classes in Adobe® Flash® CS4 or CS5 Professional:

  1. Select the File > Publish Settings command.

  2. Click the Settings button for ActionScript 3.0. Select Library Path.

  3. Click the Browse to SWC button and browse to the AIK folder in your Flash Professional installation folder.

  4. Within this folder, find the /frameworks/libs/air/aircore.swc (for AIR 2) or /frameworks/libs/air/servicemonitor.swc (for AIR 1.5).

  5. Click the OK button.

  6. Add the following import statement to your ActionScript 3.0 code:
    import air.net.*;

Flash CS3 Professional

To use these classes in Adobe® Flash® CS3 Professional, drag the ServiceMonitorShim component from the Components panel to the Library. Then, add the following import statement to your ActionScript 3.0 code:

import air.net.*;

HTTP monitoring

The URLMonitor class determines if HTTP requests can be made to a specified address at port 80 (the typical port for HTTP communication). The following code uses an instance of the URLMonitor class to detect connectivity changes to the Adobe website:

<script src="servicemonitor.swf" type="application/x-shockwave-flash" /> 
 
<script> 
    var monitor; 
    function test() 
    { 
        monitor = new air.URLMonitor(new air.URLRequest('http://www.example.com')); 
        monitor.addEventListener(air.StatusEvent.STATUS, announceStatus); 
        monitor.start(); 
    } 
    function announceStatus(e) { 
        air.trace("Status change. Current status: " + monitor.available); 
    } 
</script>

Socket monitoring

AIR applications can also use socket connections for push-model connectivity. Firewalls and network routers typically restrict network communication on unauthorized ports for security reasons. For this reason, developers must consider that users do not always have the capability to make socket connections.

The following code uses an instance of the SocketMonitor class to detect connectivity changes to a socket connection. The port monitored is 6667, a common port for IRC:

<script src="servicemonitor.swf" type="application/x-shockwave-flash" /> 
 
<script> 
    function test() 
    { 
        socketMonitor = new air.SocketMonitor('www.example.com', 6667); 
        socketMonitor.addEventListener(air.StatusEvent.STATUS, socketStatusChange); 
        socketMonitor.start(); 
    } 
    function announceStatus(e) { 
        air.trace("Status change. Current status: " + socketMonitor.available); 
    } 
</script> 

If the socket server requires a secure connection, you can use the SecureSocketMonitor class instead of SocketMonitor.

Domain Name System (DNS) records

You can look up DNS resource records using the DNSResolver class. DNS resource records provide information like the IP address of a domain name and the domain name of an IP address. You can look up the following types of DNS resource records:

  • ARecord—IPv4 address for a host.

  • AAAARecord—IPv6 address for a host.

  • MXRecord—mail exchange record for a host.

  • PTRRecord—host name for an IP address.

  • SRVRecord—service record for a service.

To look up a record, you pass a query string and the class object representing the record type to the lookup() method of the DNSResolver object. The query string to use depends on the record type:

Record class

Query string

Example query string

ARecord

host name

“example.com”

AAAARecord

host name

“example.com”

MXRecord

host name

“example.com”

PTRRecord

IP address

“208.77.188.166”

SRVRecord

Service identifier: _service._protocol.host

“_sip._tcp.example.com”

The following code example looks up the IP address of the host “example.com”.

<html> 
<head> 
<script src="AIRAliases.js"></script> 
<script language="javascript"> 
 
         
        function dnsLookup() 
        { 
            var resolver = new air.DNSResolver(); 
            resolver.addEventListener( air.DNSResolverEvent.LOOKUP, lookupComplete ); 
            resolver.addEventListener( air.ErrorEvent.ERROR, lookupError ); 
            resolver.lookup( "example.com.", air.ARecord ); 
        } 
         
        function lookupComplete( event ) 
        { 
            air.trace( "Query string: " + event.host ); 
            air.trace( "Record count: " + event.resourceRecords.length ); 
            for( var i = 0; i < event.resourceRecords.length; i++ ) 
            { 
                air.trace( event.resourceRecords[i].address ); 
            } 
             
        } 
        function lookupError( error ) 
        { 
            air.trace("Error: " + error.text ); 
        } 
</script> 
</head> 
<body onload="dnsLookup()"> 
</body> 
</html>

For more information, see:

// Ethnio survey code removed