Welcome!

AJAX & REA Authors: John Funnell, Bob Little, Kevin Hoffman, Maureen O'Gara, Onkar Singh

Article

How to monitor the connection against TCP/IP socket endpoints in AIR using ActionScript 3

Monitoring connections against TCP/IP socket endpoints using JavaScript

Today I've posted on Comtaste's blog how to monitor the connection against TCP/IP socket endpoints in AIR using JavaScript. Adobe AIR gives you the ability to use socket connections for push-model connectivity. In this context it can be necessary to have to connect to a specific port of a server. A classic scenario is the one in which the application has to send an email using the Simple Mail Transfer Protocol (SMTP). This text protocol initiates a TCP connection to the server’s port 25.
For a this application is crucial to check and monitor the the connection against TCP/IP socket endpoints.
The AIR service monitor framework can be used for this purposes. The SocketMonitor class, a subclass of the ServiceMonitor, is used to detect connectivity. The following code snippet checks the availability of the connection to a socket server running on localhost over port 25 and, if the trial has been successful, it connects to the socket server:

conn = new Socket();
// Create the istance of the SocketMonitor class
// to monitor port 25 on localhost server
monitor = new SocketMonitor( "localhost", 25 );

conn.addEventListener(Event.CONNECT, onConnect);
monitor.addEventListener(StatusEvent.STATUS, onSocketStatus);

// To start the SocketMonitor class
monitor.start();

The service monitor framework is contained within the external resides in the file servicemonitor.swc. file. In order to be used, must be imported into the the AIR project. While Flex Builder includes this automatically, if you're using the Adobe Flash IDE to create the AIR application, you need to import the ServiceMonitorShim.swc file into the Library of the Flash project in your AIR application package.

This is the complete ActionScript 3 class that check and monitor for the connection against TCP/IP socket endpoints:

package com.oreilly.aircookbook.ch15
{
import air.net.SocketMonitor;

import flash.display.Sprite;
import flash.events.*;
import flash.net.Socket;

import mx.controls.Alert;

public class ConnectSocket extends Sprite
{
private var monitor:SocketMonitor;
private var conn:Socket;
private var _isConnected:String;

public function ConnectSocket()
{
super();
// Create the istance of the Socket class
conn = new Socket();
// Create the istance of the SocketMonitor class
// to monitor port 25 on localhost server
monitor = new SocketMonitor( "localhost", 25 );

conn.addEventListener(Event.CONNECT, onConnect);
monitor.addEventListener(StatusEvent.STATUS, onSocketStatus);

// To start the SocketMonitor class
monitor.start();

}

private function onConnect(event:Event):void
{
trace( "Connection to port 25 established !" );
// You are now ready to send and receive data

}

private function onSocketStatus(e:StatusEvent):void
{
// it returns a Boolean
isConnected = monitor.available.toString();

// it returns Service.available
// isConnected = event.code;

if (monitor.available)
{
conn.connect( "localhost", 25 );

} else {

// If you're using the Flex SDK
//Alert.show( "Connection to port 25 NOT established !" );

// If you're using the Flash IDE
trace( "Connection to port 25 NOT established !" );
}
}

public function get isConnected():String
{
return _isConnected;
}

[Bindable]
public function set isConnected(_isConnected:String):void
{
this._isConnected = _isConnected;
}

}
}

The heart of the class is in the constructor of the ConnectSocket() class, which is a subclass of the Sprite class.
Once the private variable has been declared for the class, we create the instance of the Socket() e SocketMonitor()classes.
For the latter we specify the host and port property:

monitor = new SocketMonitor( "localhost", 25 );

Then we create two event listeners on the events of Event.CONNECT for the Socket class and StatusEvent.STATUS for the SocketMonitor class.
In the event handler of the STATUS event, the available property is checked, and returns a Boolean according to the result obtained by the connection test.