Previous Page
Next Page

24.0. Introduction

Socket connections allow the Flash Player to send and load data from a server over a specified network port. The main difference between socket and server connections in Chapter 20 is that socket connections don't automatically close after data transfer is complete.

When a socket connection is made, the connection stays open until the client (the Flash Player) or the server explicitly closes it. Because of this, sockets enable a special type of communication called data push, which means that the server sends information to the Flash Player at any time without a request coming from the Player itself.

Socket connections are typically used to create multiuser applications. An example of one such application would be an online chat room. The chat program might consist of a central chat server with various connected Flash Player clients. Each time a client .swf sends a message to the server, the server determines which client should receive the message and pushes the message to that specific client over the open connection. In this case, the receiving client didn't ask for the message, but rather the message was simply pushed out to it by the server. When a client closes a connection, the server notifies the other clients that someone has logged off of the system.

Two types of socket connections can be made from the Flash Player to a socket server. They are very similar in behavior and operation, but have a few subtle differences. The first is an XML socket connection that is similar to the XMLSocket in previous versions of the Flash Player. New for Flash Player 9 is a binary socket connection.

To create an XML socket connection, use the flash.net.XMLSocket class. To create a binary socket connection, use the flash.net.Socket class.

XML socket connections are focused around text. The client and server communicate by exchanging XML packets containing data. Actions are carried out by analyzing the contents of the XML packets.

Binary socket connections are new in ActionScript 3.0 and enable raw connections that allow for transfer of binary information. Binary sockets are slightly more advanced than XML sockets because they require a low-level knowledge of binary datatypes, but they are also more powerful because you can connect to a wider range of socket servers and generally do more with them. For example, binary sockets allow you to connect to mail servers (via POP3, SMTP, and IMAP), news servers (via NNTP), chat servers, or even implement screen sharing and remote desktop applications by connection to a VNC server (via RFB).

Regardless of which socket connection type you use, the connection behaves in an asynchronous manner. This means that you can't read data from the socket connection until you have been notified that data is available via an event handler. All data reading must be done in an event handler that executes automatically when data is available. Synchronous socket connections, when attempting to read data from the socket causes your program to wait until data becomes available in the socket, are easier to program but not as efficient. You'll learn more about how to deal with the asynchronous nature of the Flash Players socket connections as you go through this chapter.


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)