Basics of networking and communication

Introduction to networking and communication

When you build more complex ActionScript applications, you often need to communicate with server-side scripts, or load data from external XML or text files. The package contains classes to send and receive data across the Internet—for example, to load content from remote URLs, to communicate with other Flash Player or AIR instances, and to connect to remote websites.

In ActionScript 3.0, you can load external files with the URLLoader and URLRequest classes. You then use a specific class to access the data, depending on the type of data that was loaded. For instance, if the remote content is formatted as name-value pairs, you use the URLVariables class to parse the server results. Alternatively, if the file loaded using the URLLoader and URLRequest classes is a remote XML document, you can parse the XML document using the XML class’s constructor, the XMLDocument class’s constructor, or the XMLDocument.parseXML() method. This allows you to simplify your ActionScript code because the code for loading external files is the same whether you use the URLVariables, XML, or some other class to parse and work with the remote data.

The package also contains classes for other types of remote communication. These include the FileReference class for uploading and downloading files from a server, the Socket and XMLSocket classes that allow you to communicate directly with remote computers over socket connections, and the NetConnection and NetStream classes, which are used for communicating with Flash-specific server resources (such as Flash Media Server and Flash Remoting servers) as well as for loading video files.

Finally, the package includes classes for communication on the users’ local computer. These include the LocalConnection class, which allows you to communicate between two or more SWF files running on a single computer, and the SharedObject class, which allows you to store data on a user’s computer and retrieve it later when they return to your application.

Common networking and communication tasks

The following list describes the most common things you’ll want to do related to external communication from ActionScript; these tasks are described in this chapter:

  • Loading data from an external file or server script

  • Sending data to a server script

  • Communicating with other local SWF files

  • Working with binary socket connections

  • Communicating with XML sockets

  • Storing persistent local data

  • Uploading files to a server

  • Downloading files from a server to the user’s machine

Important concepts and terms

The following reference list contains important terms that you will encounter in this chapter:

  • External data: Data that is stored in some form outside of the SWF file, and loaded into the SWF file 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 a program such as Flash Player 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 ActionScript 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.

Working with IPv6 addresses

Flash Player and later support IPv6 (Internet Protocol version 6). IPv6 is a version of Internet Protocol that supports 128-bit addresses (an improvement on the earlier IPv4 protocol that supports 32-bit addresses). You might need to activate IPv6 on your networking interfaces. For more information, see the Help for the operating system hosting the data.

If IPv6 is supported on the hosting system, you can specify numeric IPv6 literal addresses in URLs enclosed in brackets ([]), as in the following:


Flash Player returns literal IPv6 values, according to the following rules:

  • Flash Player returns the long form of the string for IPv6 addresses.

  • The IP value has no double-colon abbreviations.

  • Hexadecimal digits are lowercase only.

  • IPv6 addresses are enclosed in square brackets ([]).

  • Each address quartet is output as 0 to 4 hexadecimal digits, with the leading zeros omitted.

  • An address quartet of all zeros is output as a single zero (not a double colon) except as noted in the following list of exceptions.

The IPv6 values that Flash Player returns have the following exceptions:

  • An unspecified IPv6 address (all zeros) is output as [::].

  • The loopback or localhost IPv6 address is output as [::1].

  • IPv4 mapped (converted to IPv6) addresses are output as [::ffff:a.b.c.d], where a.b.c.d is a typical IPv4 dotted-decimal value.

  • IPv4 compatible addresses are output as [::a.b.c.d], where a.b.c.d is a typical IPv4 dotted-decimal value.

Working through in-chapter examples

While you’re working through this chapter you might want to test the example code listings. Several of the code listings in the chapter load external data or perform some other type of communication; often these samples include trace() function calls, so the results of running the example are displayed in the Output panel. Other examples actually perform some function, such as uploading a file to a server. Testing those examples will involve interacting with the SWF and confirming that they perform the action they claim to perform.

The code examples fall into two categories. Some of the example listings are written assuming the code is in a standalone script, such as attached to a keyframe in a Flash document. To test those examples:

  1. Create a new Flash document.

  2. Select the keyframe on Frame 1 of the Timeline, and open the Actions panel.

  3. Copy the code listing into the Script pane.

  4. From the main menu, choose Control > Test Movie to create the SWF file and test the example.

Other example code listings are written as a class; the expectation is that the example class will serve as the document class for the Flash document. To test those examples:

  1. Create an empty Flash document and save it to your computer

  2. Create a new ActionScript file and save it in the same directory as the Flash document. The file’s name should match the name of the class in the code listing. For instance, if the code listing defines a class named “UploadTest,” save the ActionScript file as “”.

  3. Copy the code listing into the ActionScript file and save the file.

  4. In the Flash document, click on a blank part of the Stage or pasteboard to activate the document Property inspector.

  5. In the Property inspector, in the Document Class field, enter the name of the ActionScript class you copied from the text.

  6. Run the program using Control > Test Movie and test the example.

Finally, some of the examples in the chapter involve interacting with a program running on a server. These examples include code that can be used to create the necessary server program to test the example; you will need to set up the appropriate applications on a web server computer in order to test those examples.