2 Replies Latest reply on Aug 24, 2007 12:24 PM by fgauer1

    Help! XMLSocket does not fire events

    fgauer1
      Hi - I'm beating my head against a wall!!!!! I have a very simple XMLSocket .as file that never fires any events. I've written a very simple TCP Server in C# that receives a valid connection from my little flex app - but after that I can't get anything to go. I can't even get the onConnect event to fire.

      The TCP server works fine in C#. I can send stuff up to it using other TCP connection programs and it works well. What happens in Flex is this.

      1) The flex client connects to the TCP and sends the string <policy-file-request/>. The TCP server receives it just fine and displays it for me no problem.

      After that - nothing. No events fire on the Flex client side at all. Nothing. It hangs. I can't ping back to the client, i don't get an onConnect event nothing. HELP!!!

      Can someone give me something to try here? I really need help on this...Thanks!

      Here is the code:

      package {
      import flash.display.Sprite;
      import flash.events.Event;
      import flash.text.TextField;
      import flash.net.XMLSocket;
      import flash.xml.XMLDocument;
      import flash.events.DataEvent;
      import flash.events.SecurityErrorEvent;
      import flash.events.IOErrorEvent;

      public class SocketTest extends Sprite {


      private var tf:TextField;


      // constructor method
      public function SocketTest() {
      init();


      } // end SocketTest

      private function init():void {


      tf = new TextField();
      tf.x = 10;
      tf.y = 10;
      tf.border = true;
      addChild(tf);


      // connect to my tcp server
      var socket:XMLSocket = new XMLSocket();
      socket.connect("127.0.0.1", 10116);

      socket.addEventListener(Event.CONNECT, onConnect);
      socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      socket.addEventListener(DataEvent.DATA, onData);
      socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

      } // end init


      // NONE OF THESE EVENTS EVER FIRE BUT MY SERVER SAYS I HAVE A VALID CONNECTION
      // AND THE SERVER GETS THE STRING <policy-file-request/>!!!!

      private function ioErrorHandler(event:IOErrorEvent):void {
      trace("ioErrorHandler: " + event);
      tf.text = "error";
      }

      private function onConnect(e:Event):void {


      tf.text = "connected";

      }

      private function onData(e:DataEvent):void {

      tf.text = "fired";

      }

      private function securityErrorHandler(event:SecurityErrorEvent):void {
      tf.text = "securityErrorHandler: " + event;
      }

      public function onEnterFrame(e:Event):void {

      }

      }

      }
        • 1. Re: Help! XMLSocket does not fire events
          Peter deHaan Level 4
          fgauer1,

          This is just a shot in the dark, but I'd start by moving the "socket" declaration out of the init() method and into a class level. Something like:

          [code]
          private var tf:TextField;
          private var socket:XMLSocket;

          //...

          private function init():void {
          //...
          // connect to my tcp server
          socket = new XMLSocket();
          socket.connect("127.0.0.1", 10116);
          //...
          }
          [/code]

          I wonder if the socket is just going out of scope or getting garbage collected.

          Peter
          • 2. Re: Help! XMLSocket does not fire events
            fgauer1 Level 1
            Hi Peter,

            Thanks for your response. Somehow I managed to get this to work. Now I have a fully functioning C# TCP server and flash events are firing.

            What I did was I forced the first response down to the flex client to be a policy file (xml file). I scanned around and found the required format and sent down the policy file to open up the right port on my local machine. That got the onConnect event to fire (once I sent down the policy file in the correct format). Next, I experimented with multiple sends down to the client, and the event fired if I sent down properly formatted XML with a correct termination character 0x00 (null character).

            Thanks again for your response!!!

            fgauer