Question

I have created a flex app that uses sockets. I published the flex app in a web application that runs on glassfish server. Now from that flex app i create a socket connection to a C# server and start sending/receiving data. The problem is that after i create the socket connection to C# server the flex app first checks the policy file, and after it get's it, it closes the socket, without keep the connection alive.

This is my C# server:

TcpListener tcpListener = new TcpListener(IPAddress.Parse("172.17.41.211"), 12345);
TcpClient tcpclient = tcpListener.AcceptTcpClient();

Socket client = tcpclient.Client;
while (client.Available > 0)
{
   int bytes = 0;
   byte[] m_aBuffer = new byte[1024];
   bytes = client.Receive(m_aBuffer, m_aBuffer.Length, SocketFlags.None);
   String str = Encoding.ASCII.GetString(m_aBuffer, 0, bytes);
   if (str.StartsWith("<policy-file-request/>"))
   {
      sendBytes = Encoding.ASCII.GetBytes("<cross-domain-policy><allow-access-from domain=\"172.17.41.211\" to-ports=\"12345\"/></cross-domain-policy>\0");
      client.Send(sendBytes);
   }
}

while (client.Connected)
{
   Thread.Sleep(200);
   sendBytes = Encoding.ASCII.GetBytes("message to client");
   client.Send(sendBytes, sendBytes.Length, SocketFlags.None);
}

Now the flex client looks like:

private var socket:Socket = new Socket();
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
socket.addEventListener(ErrorEvent.ERROR, errorHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
...
socket.connect("172.17.41.211", 12345);    
...

Now after i create the connection and it gets the policy from server it closes this socket, so to be able to use this connection i have to call again

socket.connect("172.17.41.211", 12345));

After i do this, i can use normally the connection.

Can someone suggest why this happens and maybe is possible to not have closed the connection ?

Was it helpful?

Solution

You don't send the policy file through the socket itself. It needs to be on a different channel. For instance, if you connect to some ip/port, by default flash will try to connect to the same ip but on port 843 and look for the master policy file.

You can also set it manually using Security.loadPolicyFile(someURL). More information can be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top