Table of Contents

XMLSocket.connect( ) Method Flash 5

open a connection to a server application
socket.connect(host, port)

Arguments

host

A string specifying a hostname, such as "www.myserver.com", or a standard IP address (four dot-separated, 8-bit, decimal integers, such as 111.222.3.123). If null or an empty string is specified, it defaults to the server address from which the movie was served.

port

An integer specifying a TCP port number greater than or equal to 1024. There is no standard port number reserved (nor is there a widespread convention) for XMLSocket communications.

Returns

A Boolean indicating the initial success (true) or failure (false) of the connection attempt.

Description

The connect( ) method attempts to establish a connection from Flash to a server application running on host at port.

If connect( ) returns true, the initial phase of the connection completed successfully and the socket's onConnect( ) handler will be invoked at a later time. From the onConnect( ) handler, we can evaluate whether the connection was fully established. Note that connection attempts can take varying amounts of time, particularly when a connection attempt fails. You should indicate to the user that a connection attempt is in progress when invoking connect( ).

If connect( ) returns false, the initial connection phase did not complete successfully. In such a case, socket's onConnect( ) handler will not be invoked.

It is important to check both the return value of the connect( ) method and, if connect( ) returns true, the value of the success parameter sent to the onConnect( ) handler.

Usage

For security reasons, connect( ) is not permitted to connect to arbitrary Internet hosts. It can connect only to hosts in the domain from which the movie was downloaded. The cross-domain access rules for connect( ) are described under System.security.allowDomain( ). The connect( ) method returns false for connection attempts that violate security restrictions. Note that security restrictions do not apply to the Standalone Player.

Example

// Create a new socket object
mySocket = new XMLSocket();
// Assign a callback handler function to onConnect
mySocket.onConnect = handleConnect;
// Attempt to connect to an application running on myserver.com at port 10000
if (mySocket.connect("myserver.com", 10000) =  = false) {
  // Jump to a frame showing some sort of error message
  this.gotoAndStop("failureDialog");
} else {
  // Jump to a frame where we'll wait until onConnect( ) is triggered
  this.gotoAndPlay("connecting");
}

See Also

XMLSocket.close( ), XMLSocket.onConnect( )


Table of Contents