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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Sockets<br />

For more information, see:<br />

ServerSocket<br />

ServerSocketConnectEv<strong>en</strong>t<br />

Socket<br />

UDP sockets (AIR)<br />

Adobe AIR 2 and later<br />

The Universal Datagram Protocol (UDP) provides a way to exchange messages over a stateless network connection.<br />

UDP provides no guarantees that messages are delivered in order or ev<strong>en</strong> that messages are delivered at all. With UDP,<br />

the operating system’s network code usually sp<strong>en</strong>ds less time marshaling, tracking, and acknowledging messages.<br />

Thus, UDP messages typically arrive at the destination application with a shorter delay than do TCP messages.<br />

UDP socket communication is helpful wh<strong>en</strong> you must s<strong>en</strong>d real-time information such as position updates in a game,<br />

or sound packets in an audio chat application. In such applications, some data loss is acceptable, and low transmission<br />

lat<strong>en</strong>cy is more important than guaranteed arrival. For almost all other purposes, TCP sockets are a better choice.<br />

Your AIR application can s<strong>en</strong>d and receive UDP messages with the DatagramSocket and DatagramSocketDataEv<strong>en</strong>t<br />

classes. To s<strong>en</strong>d or receive a UDP message:<br />

1 Create a DatagramSocket object<br />

2 Add an ev<strong>en</strong>t list<strong>en</strong>er for the data ev<strong>en</strong>t<br />

3 Bind the socket to a local IP address and port using the bind() method<br />

4 S<strong>en</strong>d messages by calling the s<strong>en</strong>d() method, passing in the IP address and port of the target computer<br />

5 Receive messages by responding to the data ev<strong>en</strong>t. The DatagramSocketDataEv<strong>en</strong>t object dispatched for this ev<strong>en</strong>t<br />

contains a ByteArray object containing the message data.<br />

The following code example illustrates how an application can s<strong>en</strong>d and receive UDP messages. The example s<strong>en</strong>ds a<br />

single message containing the string, “Hello.”, to the target computer. It also traces the cont<strong>en</strong>ts of any messages<br />

received.<br />

Last updated 6/6/2012<br />

807

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

Saved successfully!

Ooh no, something went wrong!