Recipe 24.1.
Connecting to a Socket Server
Problem
You want
to establish a connection with a socket server.
Solution
Use either the Socket.connect( ) or XMLSocket.connect( ) method and
listen for the connect event to be notified when a
connection is made.
Discussion
To connect to a socket server, there are two
critical pieces of information that you need to know before
attempting to make a connection. The first is the domain name or IP
address of the server to which the connection will be made, and the
second is the port over which the connection should take place.
Whether you are using a Socket or an
XMLSocket instance, the connection process is exactly the
same; both classes define a connect( ) method that takes two
parameters:
host
-
A string value specifying the host to connect
to, either with a domain name such as www.example.com, or
with an IP address such as 192.168.1.101. To connect to
the web server the Flash movie is being served from, pass
null instead of a string hostname.
port
-
An int value specifying the port number
that should be used to connect to the host. The port value must be
at least 1024, unless the server has a policy file specifically
allowing ports less than 1024.
Because of the asynchronous nature of socket
programming in Flash, the connect( ) method does not wait
for a connection to happen before continuing to the next line of
ActionScript code. If you try to interact with a socket before a
connection has been fully established, you'll encounter unexpected
results and your code won't work correctly.
The proper way to connect to a socket is to
first add an event listener for the connect event before
attempting to call connect( ). The connect event is
dispatched by both Socket
and XMLSocket objects when a successful connection has been
made, letting you know that the socket is ready to be interacted
with.
The code for connecting a Socket instance
to a socket server running on localhost over port 2900
looks like this:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.Socket;
public class SocketExample extends Sprite {
private var socket:Socket;
public function SocketExample( ) {
socket = new Socket( );
// Add an event listener to be notified when the connection
// is made
socket.addEventListener( Event.CONNECT, onConnect );
// Connect to the server
socket.connect( "localhost", 2900 );
}
private function onConnect( event:Event ):void {
trace( "The socket is now connected..." );
}
}
}
If you want to connect via XMLSocket
instead, the code is almost exactly the same. First, you create the
event listener for the connect event, and then you invoke
the connect( ) method. The only difference is that all
Socket references should be replaced with
XMLSocket:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.XMLSocket;
public class SocketExample extends Sprite {
private var socket:XMLSocket;
public function SocketExample( ) {
socket = new XMLSocket( );
// Add an event listener to be notified when the connection is made
socket.addEventListener( Event.CONNECT, onConnect );
// Connect to the server
socket.connect( "localhost", 2900 );
}
private function onConnect( event:Event ):void {
trace( "The xml socket is now connected..." );
}
}
}
If the connection fails, one of two things can
happen: either the connection fails right away and a runtime error
is generated, or an ioError or securityError
event is raised to indicate that the connection could not be
completed successfully. For more information about handling the
error events, see Recipe
24.6.
Remember, when connecting to a host with a
socket connection, the following Flash Player security sandbox
rules apply:
-
The .swf and host must be in the exact
same domain in order for a successful connection to be made.
-
A .swf delivered over a network cannot
connect to a local server.
-
A local untrusted .swf cannot access any
network resources.
-
To allow cross-domain access or connections to a
port lower than 1024, a cross-domain policy file can be used.
Violating the security sandbox by attempting to
connect to an untrusted domain or on a low port raises a
securityError event. These issues can both be worked
around by using a cross-domain policy file, discussed in Recipe
3.12. To use a cross-domain policy file with either a
Socket or XMLSocket object, you need to load the
policy file by using flash.system.Security.loadPolicyFile(
), as shown here:
Security.loadPolicyFile("http://www.rightactionscript.com/crossdomain.xml");
When assembling the cross-domain policy file,
you should specify not only the allowed domains, but also the
allowed ports. If you do not specify allowed ports, Flash Player
assumes that port 80 (standard HTTP port) is the only allowed port.
You can specify a comma-delimited list using the port
attribute of the <allow-access-from> tag. The
following policy file allows all domains to connect to ports 80 and
110 (standard HTTP and POP mail ports):
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" to-ports="80,110" />
</cross-domain-policy>
See Also
Recipes 3.12,
24.5,
and 24.6
|