13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Sockets<br />

The following restrictions apply to how and where cont<strong>en</strong>t in Flash Player or in an AIR application outside of the<br />

application security sandbox can use an XMLSocket object to connect to the server:<br />

For cont<strong>en</strong>t outside of the application security sandbox, the XMLSocket.connect() method can connect only to<br />

TCP port numbers greater than or equal to 1024. One consequ<strong>en</strong>ce of this restriction is that the server daemons<br />

that communicate with the XMLSocket object must also be assigned to port numbers greater than or equal to 1024.<br />

Port numbers below 1024 are oft<strong>en</strong> used by system services such as FTP (21), Telnet (23), SMTP (25), HTTP (80),<br />

and POP3 (110), so XMLSocket objects are barred from these ports for security reasons. The port number<br />

restriction limits the possibility that these resources will be inappropriately accessed and abused.<br />

For cont<strong>en</strong>t outside of the application security sandbox, the XMLSocket.connect() method can connect only to<br />

computers in the same domain where the cont<strong>en</strong>t resides. (This restriction is id<strong>en</strong>tical to the security rules for<br />

URLLoader.load().) To connect to a server daemon running in a domain other than the one where the cont<strong>en</strong>t<br />

resides, you can create a cross-domain policy file on the server that allows access from specific domains. For details<br />

on cross-domain policy files, see “AIR security” on page 1065.<br />

Note: Setting up a server to communicate with the XMLSocket object can be chall<strong>en</strong>ging. If your application does not<br />

require real-time interactivity, use the URLLoader class instead of the XMLSocket class.<br />

You can use the XMLSocket.connect() and XMLSocket.s<strong>en</strong>d() methods of the XMLSocket class to transfer XML to<br />

and from a server over a socket connection. The XMLSocket.connect() method establishes a socket connection with<br />

a web server port. The XMLSocket.s<strong>en</strong>d() method passes an XML object to the server specified in the socket<br />

connection.<br />

Wh<strong>en</strong> you invoke the XMLSocket.connect() method, the application op<strong>en</strong>s a TCP/IP connection to the server and<br />

keeps that connection op<strong>en</strong> until one of the following occurs:<br />

The XMLSocket.close() method of the XMLSocket class is called.<br />

No more refer<strong>en</strong>ces to the XMLSocket object exist.<br />

Flash Player exits.<br />

The connection is brok<strong>en</strong> (for example, the modem disconnects).<br />

Connecting to a server with the XMLSocket class<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

To create a socket connection, you must create a server-side application to wait for the socket connection request and<br />

s<strong>en</strong>d a response to the Flash Player or AIR application. This type of server-side application can be writt<strong>en</strong> in AIR or in<br />

another programming language such as Java, Python, or Perl. To use the XMLSocket class, the server computer must<br />

run a daemon that understands the simple protocol used by the XMLSocket class:<br />

XML messages are s<strong>en</strong>t over a full-duplex TCP/IP stream socket connection.<br />

Each XML message is a complete XML docum<strong>en</strong>t, terminated by a zero (0) byte.<br />

An unlimited number of XML messages can be s<strong>en</strong>t and received over a single XMLSocket connection.<br />

Creating and connecting to a Java XML socket server<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The following code demonstrates a simple XMLSocket server writt<strong>en</strong> in Java that accepts incoming connections and<br />

displays the received messages in the command prompt window. By default, a new server is created on port 8080 of<br />

your local machine, although you can specify a differ<strong>en</strong>t port number wh<strong>en</strong> starting your server from the command<br />

line.<br />

Last updated 6/6/2012<br />

802

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!