Adobe Flex 3 Help

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:

  • 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 9.0 and later versions supports 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:

rtmp://[2001:db8:ccc3:ffff:0:444d:555e:666f]:1935/test

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.