Macromedia Flash MX ActionScripting: Advanced Training from the Source

A socket server is an application that can accept "socket" connections. Socket connections are persistent which means they let you remain connected to a server, rather than making a connection just long enough to download information and then disconnecting. Unlike a scripted page, a socket server is an application that's always running. It can accept numerous simultaneous connections and exchange information between them. Thus, while you're connected to a socket server, you can send or receive information any time. Using socket connections to continually transfer data to and from the server is how most chats and multiplayer games are created in Flash.

A key thing to understand about using socket connections with Flash is that you don't have to request information to get it for example, in a chat application a message can be pushed into Flash at any time without Flash having to ask for it.

You cannot, however, just drop a socket server into the CGI-bin of your Web site or place it in a normal Web-accessible directory. Usually written in Java, C, C++, or Visual Basic, socket servers require root-level access to the Web server which usually means you need to be running your own dedicated server to install and use a socket server. You can, however, set up a socket server on your own personal computer so that you can develop with it.

For the next exercise, we'll show you how to get a server up and running on your local machine so that you can go on to build a simple chat application that connects to a socket server. To test it, you'll need to use Windows 98, Windows 2000, Windows XP, or Mac OS X.

The accompanying CD-ROM contains a simple Java-based socket server, called AquaServer. (It was written by Branden Hall of Figleaf Software.) You need to have Java 2 Runtime Environment (JRE) version 1.3.1 or higher installed on your machine to run our socket server, as well as to test the chat program you build in the next section. (Mac OS X comes with the correct version of JRE installed.)

  1. Download and install the JRE (for Windows users), by going to http://www.sun.com. Click on Downloads > Java Technology > Java 2 Runtime Environment v 1.3.1 (or higher). Download the installer and follow the instructions to install it.

    In the next section, we'll tell you to start the server on a specific port. This is what you must do to start the server on a specific port on Windows:

  2. Copy all of the files for this lesson from the CD-ROM into a directory on your hard drive. Open the MS-DOS prompt or Command Prompt (depending on your Windows version) by selecting Start > All Programs (or Program Files) > Accessories > Command Prompt (or MS-DOS).

  3. Navigate to the directory that contains the files for this lesson by typing cd and the full path to that directory (for example, cd C:\Documents and Settings\Jobe Makar\Desktop\FlashFiles). Then press the Enter key.

    The letters cd stand for change directory.

  4. In the console window, type java AquaServer 9999 and press the Enter key.

    This statement is case-sensitive, so make sure you type it exactly as written above. The console window should display information telling you that the server has started running and is listening on port 9999.

    If you installed the JRE properly, this should work without a hitch, though you must leave the console window open for as long as you wish the server to run. When you close the window, the socket server will stop running.

    AquaServer is one of a few free socket servers designed expressly for work with Flash. Although you cannot currently use any free socket server for commercial purposes, there are some extremely fast and stable commercial sockets servers developed specifically for Flash, including the following

    • ElectroServer: www.electrotank.com/ElectroServer

    • Unity: www.moock.org/unity/

Категории