Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
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 Microphone capabilities in Adobe AIR 2
  • Using the SQLite database access API in Adobe AIR
  • Writing multiscreen AIR apps
  • User experience considerations with SQLite operations
  • Exploring the new file capabilities in Adobe AIR 2
  • Reducing CPU usage in Adobe AIR
  • Building multilingual Flex applications on Adobe AIR
  • Resolving DNS records in Adobe AIR 2
  • Using drag-and-drop support of remote files in Adobe AIR 2
  • Retrieving a list of network interfaces 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
04/11/2012 Surround sound 5.1 with Air 3.2 on desktop app?
12/12/2011 Live Streaming H.264 Video on iOS using AIR
04/17/2012 HTMLLoader - Google Maps?
04/12/2012 Tabindex in forms on mobile?

Adobe AIR Blog

More
07/09/2012 Protected: Publishing Adobe AIR 3.0 for TV on Reference Devices
07/08/2012 Source Code: Adobe AIR 3.3 Retina Video Application
07/06/2012 Application specific File Storage on Adobe AIR based ios Application
07/04/2012 Recent Work - iPad/Android App: Inside My toyota

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

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

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • 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
  • Security
  • 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
  • 台灣

Southeast Asia

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

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement