User Guide

386 Networking and Communication
Socket class
Introduced in ActionScript 3.0, the Socket class enables ActionScript to make socket
connections and to read and write raw binary data. It is similar to the XMLSocket class, but
does not dictate the format of the received and transmitted data. The Socket class is useful for
interoperating with servers that use binary protocols. By using binary socket connections, you
can write code that allows interaction with several different Internet protocols, such as POP3,
SMTP, IMAP, and NNTP. This in turn enables Flash Player to connect to mail and news
servers.
Flash Player can interface with a server by using the binary protocol of that server directly.
Some servers use the big-endian byte order, and some use the little-endian byte order. Most
servers on the Internet use the big-endian byte order because “network byte order” is big-
endian. The little-endian byte order is popular because the Intel x86 architecture uses it. You
should use the endian byte order that matches the byte order of the server that is sending or
receiving data. All operations that are performed by the IDataInput and IDataOutput
interfaces, and the classes that implement those interfaces (ByteArray, Socket, and
URLStream), are encoded by default in big-endian format; that is, with the most significant
byte first. This is done to match Java and official network byte order. To change whether big-
endian or little-endian byte order is used, you can set the
endian property to
Endian.BIG_ENDIAN or Endian.LITTLE_ENDIAN.
XMLSocket class
ActionScript provides a built-in XMLSocket class, which lets you open a continuous
connection with a server. This open connection removes latency issues and is commonly used
for real-time applications such as chat applications or multiplayer games. A traditional
HTTP-based chat solution frequently polls the server and downloads new messages using an
HTTP request. In contrast, an XMLSocket chat solution maintains an open connection to
the server, which lets the server immediately send incoming messages without a request from
the client.
TIP
The Socket class inherits all the methods implemented by the IDataInput and
IDataOutput interfaces (located in the flash.utils package), and those methods should be
used to write to and read from the Socket.