1 Reply Latest reply on Jul 12, 2007 3:50 PM by Venkat_G

    XMLSocket Events not received

    Venkat_G Level 1
      I am trying to get a simple ActionScript 3 class to connect to a custom Java Server. The server receives the connection but no events are triggered on the ActionScript side. I am serving a policy file and loading it explicitly on the ActionScript side

      The simplest code to reproduce this is as follows:

      =====
      Java code:
      =====
      import java.io.*;
      import java.net.*;

      class SimpleServer
      {
      public static void main(String[] args)
      {
      System.out.println(">> Starting SimpleServer");
      try
      {
      int ch;
      System.out.println("Opening server socket at port 8080");
      ServerSocket socket = new ServerSocket(8080);

      System.out.println("Waiting for connection at port 8080");
      Socket incoming = socket.accept();
      System.out.println("Accepted Connection. Waiting for text");

      BufferedReader readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      PrintStream printOut = new PrintStream(incoming.getOutputStream());
      System.out.println("Read: ");
      while ((ch = readerIn.read()) > 0)
      System.out.println("Read: ");
      while ((ch = readerIn.read()) > 0)
      {
      System.out.print((char) ch);
      }
      System.out.println("\nDone");

      System.out.println("Outputting policy");
      printOut.println("<?xml version=\"1.0\"?>\r\n<!DOCTYPE cross-domain-policy SYSTEM \" http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\r\n<cross-domain-policy><all ow-access-from domain=\"*\" secure=\"false\" to-ports=\"8080,8081\"/></cross-domain-policy>\0");

      System.out.println("Closing connections");
      incoming.close();
      socket.close();
      System.out.println("Closed sockets at port 8080");

      System.out.println("Opening server socket at port 8081");
      socket = new ServerSocket(8081);

      System.out.println("Waiting for connection");
      incoming = socket.accept();
      System.out.println("Waiting for connection");
      incoming = socket.accept();
      System.out.println("Accepted Connection at port 8081");

      System.out.println("isConnected: " + incoming.isConnected());

      readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
      printOut = new PrintStream(incoming.getOutputStream());
      System.out.println("Sending <hello/>");
      printOut.println("<hello/>\0");
      System.out.println("Sent");
      System.out.println("isConnected: " + incoming.isConnected());

      System.out.println("Read: ");
      while ((ch = readerIn.read()) > 0)
      {
      System.out.print((char) ch);
      }
      System.out.println("\nDone");
      System.out.println("isConnected: " + incoming.isConnected());

      System.out.println("Sending <hello/>");
      printOut.println("<hello/>\0");
      System.out.println("Sent");
      System.out.println("isConnected: " + incoming.isConnected());

      System.out.println("Closing connections");
      incoming.close();
      socket.close();
      System.out.println("Closed sockets at port 8080");
      }
      catch (Exception e)
      {
      System.out.println(e);
      }
      }
      }

      =====
      ActionScript code:
      =====
      package
      {
      import flash.display.Sprite;

      public class SockTest extends Sprite
      {
      import flash.net.XMLSocket;
      import flash.system.Security;
      import flash.events.*;
      import flash.utils.Timer;

      public function SockTest()
      {
      trace("Sandbox type: " + Security.sandboxType);
      trace("Loading policy file");
      Security.loadPolicyFile("xmlsocket://127.0.0.1:8080");
      trace("Done calling loadPolicyFile");

      var timer:Timer = new Timer(5000, 1);
      timer.addEventListener("timer", timerHandler);
      timer.start();
      }

      private function timerHandler(event:TimerEvent):void
      {
      trace("Creating XMLSocket");
      var xmlSocket:XMLSocket = new XMLSocket();

      trace("Calling connect");
      xmlSocket.connect("127.0.0.1", 8081);
      trace("Done calling connect");

      trace("Adding event listeners");
      xmlSocket.addEventListener(Event.CONNECT, connectHandler);
      xmlSocket.addEventListener(Event.CLOSE, closeHandler);
      xmlSocket.addEventListener(DataEvent.DATA, dataHandler);
      xmlSocket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      xmlSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
      trace("Done adding event listeners");

      trace("willTrigger(Event.CONNECT)?: " + xmlSocket.willTrigger(Event.CONNECT));
      trace("xmlSocket.connected="+xmlSocket.connected);

      xmlSocket.send(new XML("<hello/>"));

      trace("Done sending");
      }

      public function connectHandler(event:Event):void
      {
      trace("connectHandler: " + event);
      }

      private function closeHandler(event:Event):void
      {
      trace("closeHandler: " + event);
      }

      private function dataHandler(event:DataEvent):void
      {
      trace("dataHandler: " + event);
      }

      private function ioErrorHandler(event:IOErrorEvent):void
      {
      trace("ioErrorHandler: " + event);
      }

      private function securityErrorHandler(event:SecurityErrorEvent):void
      {
      trace("securityErrorHandler: " + event);
      }
      }
      }
      =====
      ActionScript compiled using
      mxmlc -use-network=true SockTest.as
      =====
      Java side result:
      =====
      >> Starting SimpleServer
      Opening server socket at port 8080
      Waiting for connection at port 8080
      Accepted Connection. Waiting for text
      Read:
      <policy-file-request/>
      Done
      Outputting policy
      Closing connections
      Closed sockets at port 8080
      Opening server socket at port 8081
      Waiting for connection
      Accepted Connection at port 8081
      isConnected: true
      Sending <hello/>
      Sent
      isConnected: true
      Read:

      Done
      isConnected: true
      Sending <hello/>
      Sent
      isConnected: true
      Closing connections
      Closed sockets at port 8080

      =====
      ActionScript side trace output: [From debug version of standalone flash player version 9,0,31,0 on Linux]
      =====
      Sandbox type: localWithNetwork
      Loading policy file
      Done calling loadPolicyFile
      Creating XMLSocket
      Calling connect
      Done calling connect
      Adding event listeners
      Done adding event listeners
      willTrigger(Event.CONNECT)?: true
      xmlSocket.connected=false
      Done sending
      =====
      As you can see, no events are fired. I do know that the policy file is having effect because if I try to connect to any other port, I get a security exception but here again the SecurityErrorEvent is not fired. Also, the data sent from the server is not triggering any DataEvent either.

      How do I get the events to fire?

      Thanks,
      Venkat
        • 1. Re: XMLSocket Events not received
          Venkat_G Level 1
          I got it working finally although I dont understand why it makes a difference.

          I made the

          var xmlSocket:XMLSocket

          an instance variable instead of declaring it inside the timerHandler method.

          I dont understand why this needed. The timer works fine (timerHandler is invoked) even when it is a local variable.

          Venkat