Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 sockets and policy file request to a C# server

This problem is driving me crazy! I've read all the questions on Stack Overflow but I'm still stuck.

My as3 program works very well, but when I have finished it and put it on a server, it starts to request this famous policy file.

AS3 script:

socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);

socket.connect( MYHOST, 4242 );

C# server code:

TcpListener serverSocket = new TcpListener(4242);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
clientSocket = serverSocket.AcceptTcpClient();
NetworkStream networkStream = clientSocket.GetStream();
StreamReader read = new StreamReader(networkStream, Encoding.UTF8);
StreamWriter write = new StreamWriter(networkStream, Encoding.UTF8);

response = read.ReadLine();

if (response.Contains("policy"))
{
 write.Write("<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0");
 write.Flush();
 clientSocket.Close();
 return;
}

So, when the AS3 doesn't find the policy on the default port 843 (or something similar), it asks directly on the same socket as the connection. My C# code sees the request and replies, after which the AS3 script closes the connection (which is OK), but it never reconnects.

I have tried to put this in the AS3 before the connect():

Security.loadPolicyFile( "xmlsocket://myhost.com:4242");

But when I do the connect() it simply gets stuck and never requests the policy file. After I close the AS3 application, my server sees the request, but the connection is closed. It's like the client forget to do a flush.

Can someone tell me how I can solve this problem correctly?

like image 874
Univers3 Avatar asked Apr 12 '13 17:04

Univers3


1 Answers

After 3 days i have finally discovered what is the bug in the code.

A bounty of 50 points and no one have noticed it :-(

Is very stupid, a novice error:

When the flash application ask for the policy file dont send the newline char, but the terminating char '\0'.

and im reading with the read.ReadLine(); that read until the '\n', so it stuck.

Thank you all for your replies.

like image 136
Univers3 Avatar answered Oct 20 '22 00:10

Univers3