Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:29:34+00:00 2026-05-21T23:29:34+00:00

I wish to make an Air 2 Server via ServerSocket class (you will find

  • 0

I wish to make an Air 2 Server via ServerSocket class (you will find below code an example about it).

Constraints :

  • Server must be in Air
  • Client must be displayed through web browser

Clients are displayed with an Web browser so when a client want to establish a connection to Air server, Flash sends a crossdomain request through socket and server sends it back but nothing hapen then.

The As3Doc specifies that when flash sends crossdomain request, server must to send it back, then Flash closes connection and open a new connection if crossdomain is ok.

I’ve tried different settings but nothing works, client never receives CONNECTED’s event.

Any ideas ?

Server side code :

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">

    <s:TextArea x="0" y="0" width="100%" height="100%" id="log"/>

    <fx:Script>
        <![CDATA[
            private var _server : ServerSocket = new ServerSocket;

            private function init() : void
            {
                _server.bind(4500, "127.0.0.1");
                _server.addEventListener(ServerSocketConnectEvent.CONNECT, onClientConnection);
                _server.listen();
            }

            private function onClientConnection(e : ServerSocketConnectEvent) : void
            {
                var socket : Socket = e.socket;
                log.appendText("Client connected : " + socket.localAddress + ":" + socket.localPort + "\n");
                socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
            }

            private function onData(e:Event) : void
            {
                var socket : Socket = e.target as Socket;
                log.appendText("Data : " + socket.readUTFBytes(socket.bytesAvailable));
                socket.writeUTF(
                    '<cross-domain-policy>' +
                    '       <allow-access-from domain="*" to-ports="4500" />' +
                    '</cross-domain-policy>'
                    + String.fromCharCode(0)
                    );
                socket.writeByte(0);
                socket.flush();
                socket.close();
            }
        ]]>
    </fx:Script>
</s:WindowedApplication>

Client side code :

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.Socket;
    import flash.text.TextField;
    import flash.utils.setTimeout;

    public class TestClient extends Sprite
    {
        private var log : TextField;
        private var _socket : Socket;

        public function TestClient()
        {
            log = new TextField;
            log.width = stage.stageWidth;
            log.height = stage.stageHeight;
            addChild(log);

            _socket = new Socket;
            _socket.addEventListener(Event.CONNECT, onConnection);
            _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
            _socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
            tryConnection();
        }

        private function tryConnection() : void
        {
            log.appendText("Try connection ... \n");
            _socket.connect("127.0.0.1", 4500);
        }

        private function onConnection(e : Event) : void
        {
            log.appendText("Connected !");
        }

        private function onError(e : Event) : void
        {
            log.appendText(e.toString() + "\n");
            setTimeout(tryConnection, 1000);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:29:35+00:00Added an answer on May 21, 2026 at 11:29 pm

    Your approach is right, but you’re using writeUTF to write down the XML policy file to the socket. writeUTF also writes the length of the string in two bytes before the actual UTF string. That is corrupting the XML policy file the client is receiving.

    Simply use writeUTFBytes instead of writeUTF and everything should work just fine.

    Also, you don’t need to writeByte like you do. Appending a null character to your policy string is enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I sometimes wish I could make special methods for certain instantiations of
I wish to know all the pros and cons about using these two methods.
I wish to implement a 2d bit map class in Python. The class would
I wish to test a function that will generate lorem ipsum text, but it
I wish to make a trigger but i'm not sure how to grab the
I wish to make use of my unlimited shared hosting to create several small
I wish to make a simple GET request to another script on a different
I wish to make a cosine table at compile time. Is there a way
I wish to make the all the list items to be centered. or at
I wish to make sure that my data has a constraint the following check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.