Adobe
製品
Creative Suiteファミリー
Photoshopファミリー
Acrobatファミリー
Flash Platform
Digital Marketing Suite
Digital Enterprise Platform
Digital Publishing Suite
その他の製品一覧
ソリューション
コンテンツオーサリング
教育
金融機関
デジタルマーケティングソリューション
その他のソリューション
ラーニング サポート ダウンロード 会社情報
ご購入
アドビストア安心のサポート& サービス
アカデミック版のご購入学生、教職員、個人
ライセンスのご購入企業、教育機関、官公庁
販売パートナー
検索
 
情報 サインイン
ようこそ、 カート 注文状況 ユーザー登録
マイアカウント
サインアウト
サインインの目的 お客様のアカウントや体験版ダウンロード、製品の拡張機能、コミュニティエリアへのアクセスなどを管理するため
Adobe
製品 セクション   検索  
ソリューション 会社情報
サポート ラーニング
サインイン ようこそ、 注文状況 ユーザー登録
Qty:
Subtotal
Checkout
Adobe Developer Connection / Adobe AIRデベロッパーセンター /

Creating a socket server in Adobe AIR 2

著者 Christian Cantrell

Christian Cantrell
  • blogs.adobe.com/cantrell

Created

17 November 2009

ページ ツール

Facebookでシェア
Twitterでツイート
LinkedInでシェア
ブックマーク
印刷

Tags

必要条件

この記事に必要な予備知識

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

ユーザーレベル

中級

必要な製品

  • 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

  • Reducing CPU usage in Adobe AIR
  • Using the SQLite database access API in Adobe AIR
  • Using drag-and-drop support of remote files in Adobe AIR 2
  • Building multilingual Flex applications on Adobe AIR
  • Retrieving a list of network interfaces in Adobe AIR 2
  • Using the Salesbuilder Adobe AIR sample application
  • User experience considerations with SQLite operations
  • Writing multiscreen AIR apps
  • Using the Microphone capabilities in Adobe AIR 2
  • Building an XML viewer on Adobe AIR with Flex

製品

  • Creative Suiteファミリー
  • Photoshopファミリー
  • Acrobatファミリー
  • Flashプラットフォーム
  • Digital Marketing Suite
  • Digital Enterprise Suite
  • Digital Publishing Suite
  • モバイルアプリ

ソリューション

  • カスタマーエクスペリエンスマネジメント
  • コンテンツオーサリング
  • デジタルマーケティング

業種別ソリューション

  • 教育
  • 金融機関

サポート

  • ヘルプ&サポート
  • 注文と返品
  • ダウンロードに関するヘルプ
  • ユーザー登録に関するヘルプ

ラーニング

  • ADC: Adobe Developer Center
  • Adobe TV
  • Design Magazine
  • Photoshop Magazine
  • Focus In

ご購入方法

  • アドビストア
  • アカデミック版のご購入
  • ライセンスのご購入

ダウンロード

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

会社情報

  • プレスルーム
  • パートナープログラム
  • 企業の社会的責任(英語)
  • 採用情報
  • 投資家の皆様へ(英語)
  • イベント&セミナー
  • Legal(英語)
  • お問い合わせ
国・地域および言語の選択 日本(変更)
国・地域および言語の選択 閉じる

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
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • España
  • France
  • Deutschland
  • Hrvatska
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Magyarország
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Österreich - Deutsch
  • Polska
  • Portugal
  • România
  • Россия
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Slovenija
  • Slovensko
  • Srbija
  • Suomi
  • Sverige
  • 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.

当Webサイトをご利用のお客様は、利用条件およびプライバシーポリシー(2011年9月30日更新)にご同意いただいたものとみなされます。

Reviewed by TRUSTe: site privacy statement