Flash CS4 Resources
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 flash.net 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 flash.net 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 flash.net 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:
Important concepts and terms
The following reference list contains important terms that you will encounter in this chapter:
Working with IPv6 addresses
Flash Player 22.214.171.124 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:
The IPv6 values that Flash Player returns have the following exceptions:
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:
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:
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.