Adobe
Products
Creative Suite
Photoshop Family
Acrobat Family
Flash Platform
Digital Marketing Suite
Digital Publishing Suite
More products
Solutions
Digital marketing solutions
Digital media solutions
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Store
Adobe Store for home and home office
Education Store for students, educators, and staff
Business Store for small and medium businesses
Other ways to buy
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections   Search  
Solutions Company
Help Learning
Sign in Welcome, My orders My Adobe
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Creating a socket server in Adobe AIR 2

by Christian Cantrell

Christian Cantrell
  • blogs.adobe.com/cantrell

Created

17 November 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR Flex networking socket

Requirements

Prerequisite knowledge

This article is intended for developers who are comfortable with ActionScript 3 and who have at least a basic understanding of TCP sockets.

User level

Intermediate

Required products

  • Adobe AIR

This article discusses the new socket server support in AIR 2. The ability to listen on a socket means that your AIR 2 applications can send and receive data over a TCP socket without having to initiate the socket connection. Being able to accept incoming socket connections never made much sense for SWF content running in the browser, but in the context of desktop applications written for Adobe AIR, it opens up all kinds of new possibilities.

This article covers the following topics:

  • The history of socket support in Flash Player
  • Introducing the SocketServer class
  • Tips on using the ServerSocket class
  • Sample application: HTTPeek

History of socket support in Flash Player

Flash Player has a long history of support for TCP sockets. The XMLSocket was first introduced in Flash Player 5. XMLSockets provide developers with an easy way to read and write XML data between a client and a server across a TCP connection. In Flash Player 9, after watching developers use the XMLSocket class to read and write all kinds of other data besides just XML, the Socket class was introduced.

The Socket class (along with ByteArrays) gives Flash developers true binary socket support, which enables them to pass data between clients and servers using protocols other than HTTP. When the Flash Platform was extended to the desktop with Adobe AIR, developers started asking for the ability to create socket endpoints so as to be able to listen on sockets. AIR 2 delivers this capability with the ServerSocket class.

Introducing the ServerSocket class

The ServerSocket class enables developers to accept incoming socket connections, as opposed to just being able to open connections from clients. In other words, using the ServerSocket class enables applications to actually become servers. Establishing a socket endpoint has several interesting use cases:

  • Inter-application communication: Applications can communicate with each other by listening on sockets, connecting to each other, and passing data back and forth.
  • Peer-to-peer applications: It's becoming increasingly useful for applications to be able to share data with each other across networks, as opposed to just with servers.
  • Additional protocol support: Protocols such as FTP may require clients to listen on specific ports so the server can connect back to the client (as in the case of active mode).
  • Network utilities: Applications like proxy servers require the use of both incoming and outgoing socket connections.

Creating a socket endpoint is as easy as creating an outgoing socket. The following code shows how to listen for incoming TCP connections:

import flash.net.ServerSocket; import flash.events.ServerSocketConnectEvent; private function onConnect(e:ServerSocketConnectEvent):void { var incomingSocket:Socket = e.socket; // You can now read and write data from the socket instance } var server:ServerSocket = new ServerSocket(); server.addEventListener(Event.CONNECT, onConnect); server.bind(8888); // Pass in the port number you want to listen on server.listen();

Tips on using the ServerSocket class

Keep in mind the following if you want your application to act as a server:

  • For security reasons, the ServerSocket class can only be used in the application sandbox.
  • Applications that attempt to bind to ports below 1024 will be subject to the operating system's security policy. On Mac OS X and Linux, these applications will need to run as root. For this reason, it's best for most applications to use nonprivileged ports.
  • The ServerSocket constructor takes a backlog argument. If not provided or set to 0 (zero), the backlog is the maximum value supported by the operating system.

HTTPeek: An AIR 2 HTTP proxy server

HTTPeek, shown in Figure 1, is a sample application I wrote to demonstrate the use of the ServerSocket class. It's an HTTP proxy which logs request and response headers of all HTTP traffic proxied through it, and is useful for debugging web applications. You can download the source code from Google Code or the AIR Developer Center.

The HTTPeek application
Figure 1. The HTTPeek application

Where to go from here

This article covered basic information about listening for incoming TCP sockets in Adobe AIR 2. For more information on using sockets in Flash Player, see:

  • Demonstration of the ServerSocket API in AIR
  • HTTPeek sample application on Google Code or the AIR Developer Center

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Using the Salesbuilder Adobe AIR sample application
  • Creating accessible Adobe AIR applications with the Flex SDK
  • Using the Adobe AIR 2 NativeProcess API to create a screen recorder
  • Building multilingual Flex applications on Adobe AIR
  • Reducing CPU usage in Adobe AIR
  • Exploring the new file capabilities in Adobe AIR 2
  • User experience considerations with SQLite operations
  • Using the SQLite database access API in Adobe AIR
  • Writing multiscreen AIR apps
  • Resolving DNS records in Adobe AIR 2

Tutorials & Samples

Tutorials

  • Retrieving a list of network interfaces in Adobe AIR 2
  • Writing multiscreen AIR apps
  • Using the AIR 2 NativeProcess API to create a screen recorder
  • Resolving DNS records in Adobe AIR 2

Samples

  • Using the Salesbuilder Adobe AIR sample application

Adobe AIR Forums

More
02/07/2012 FacebookMobile - logout user?
02/07/2012 Do app updates delete LSOs?
02/02/2012 Facebook iOS?
01/31/2012 Can't sign AIR application so it can be uploaded to Apple

Adobe AIR Blog

More
02/02/2012 AIRKinect Extension is a Native Extension for use with Adobe AIR...
02/01/2012 Microsoft Kinect and Adobe AIR
02/01/2012 New Adobe Flash Player 11.2 beta for Desktops and Adobe AIR 3.2 beta
01/30/2012 Using charts inside Mobile Applications with Adobe AIR

Products

  • Creative Suite
  • Photoshop Family
  • Acrobat Family
  • Flash Platform
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Mobile apps

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • Adobe Store
  • For students and educators
  • For small and medium businesses
  • For enterprises
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Pacific - English
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).

Ad Choices

Reviewed by TRUSTe: site privacy statement