XMLSocket events not received

I'm 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 explicitly on the side of the ActionScript

The simplest code to reproduce, it is as follows:

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

SimpleServer class
{
Public Shared Sub main (String [] args)
{
System.out.println ("> > starting SimpleServer");
Try
{
int ch;
System.out.println ("opening server socket to port 8080");
Socket ServerSocket = new ServerSocket (8080);

System.out.println ("Waiting for connection on port 8080");
Plug incoming = socket.accept ();
System.out.println ("connection accepted. Pending text");

BufferedReader readerIn = new BufferedReader (new InputStreamReader (incoming.getInputStream ()));
PrintStream print = 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 ("exit strategy");
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 > < ow-access-of all area =------" *------' course =------"false\" to-ports =------' 8080, 8081\ "/ > < / cross-domain-policy > \0");

System.out.println ("close connections");
incoming. Close();
Socket.Close ();
System.out.println ("taken to port 8080 closed");

System.out.println ("opening server socket to 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 to port 8081");

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

readerIn = new BufferedReader (new InputStreamReader (incoming.getInputStream ()));
Print = new PrintStream (incoming.getOutputStream ());
System.out.println ("sending < Hello / >");
printOut.println ("< Hello / > \0");
System.out.println ("smell");
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 ("smell");
System.out.println ("isConnected:" + incoming.isConnected ());

System.out.println ("close connections");
incoming. Close();
Socket.Close ();
System.out.println ("taken to port 8080 closed");
}
catch (System.Exception e)
{
System.out.println (e);
}
}
}

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

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

public void SockTest()
{
trace ("type of Sandbox:" + Security.sandboxType);
trace ("loading policy file");
Security.loadPolicyFile ("xmlsocket://127.0.0.1:8080"); ")
trace ("done calling loadPolicyFile");

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

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

trace ("Calling connect");
xmlSocket.connect ("127.0.0.1", 8081);
trace ("done connect call");

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 ("adding event listeners through");

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

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

trace ("done send");
}

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

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

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

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

private void securityErrorHandler(event:SecurityErrorEvent):void
{
trace ("securityErrorHandler:" + event);
}
}
}
=====
ActionScript compiled using
mxmlc-use-network = true SockTest.as
=====
Result of Java side:
=====
> > from SimpleServer
Opening of socket server on port 8080
Waiting for connection on port 8080
Accepted connection. Pending text
Read:
< policy-file-request / >
Fact
Political output
Close connections
Taken closed to port 8080
Opening of socket server to port 8081
Waiting for connection
Connection accepted to port 8081
isConnected: true
Sending of < Hello / >
Envoy
isConnected: true
Read:

Fact
isConnected: true
Sending of < Hello / >
Envoy
isConnected: true
Close connections
Taken closed to port 8080

=====
Output trace for the ActionScript side: [from the debug version of the flash player version 9,0,31,0 stand-alone on Linux]
=====
Type of sandbox: localWithNetwork
Loading policy file
Fact call loadPolicyFile
Creating XMLSocket
Call connect
Call connect
Add event listeners
Result of adding event listeners
willTrigger (Event.CONNECT)? : true
xmlSocket.connected = false
Because of the sending
=====
As you can see, no event is fired. I know that the policy file has effect because if I try to connect to any other port, I get a security exception, but here again the SecurityErrorEvent is not triggered. In addition, the data sent from the server not fire either all DataEvent.

How can I get the events to trigger?

Thank you
Vincent

I got it finally works but I do not understand why it makes a difference.

I did the

var xmlSocket:XMLSocket

an instance variable instead of declare inside the timerHandler method.

I do not understand why is this necessary. The timer works fine (timerHandler is invoked) even when it's a local variable.

Vincent

Tags: Flex

Similar Questions

Maybe you are looking for