0

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 ?

  • Can I ask *why* you're using a socket server in the first place? Is there a very specific reason or you just want a connection to your server? Because there are easier and better ways to connect client to server than creating your own socket since there are a lot of already create, very well know and proven standards out there. – J_A_X Sep 28 '11 at 01:20
  • Yes, this is something that we need to communicate with the server in a specific way. –  Sep 28 '11 at 07:05
  • Can you be more descriptive? There could very well be a better alternative. – J_A_X Sep 28 '11 at 07:24
  • We have a C# server with a custom protocol, this server runs for a while and it works fine. Now i need a client that connects to that server and receive messages in a specific format(ASCII + byte arrays, byte arrays are audio streams sent from the server like pcm raw data). When i receive message i decode it and see what to do next with it. I cannot change the server side(i know only the communication protocol), but i need the client to communicate with this server. This why i created a socket connection to a specific port. –  Sep 28 '11 at 07:29

1 Answers1

0

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.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • but this means that i have to create a separate thread that will listen on port 843 ? But aren't there any other solutions ? –  Sep 28 '11 at 08:08
  • I believe you can specify an http server for the policy file, but don't take my word on it. Look at the documentation. – J_A_X Sep 28 '11 at 08:59
  • That's the main problem, that what i concluded from the documentation is that i will have to create a separate thread for handling the request of the policy data. But i tought that i can do that witout creating this separate thread. –  Sep 28 '11 at 09:23
  • You could just specify a url and use a webserver: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Security.html#loadPolicyFile() – J_A_X Sep 28 '11 at 09:32
  • For this i need to create a web server on the server side, but i cannot do that, or it will imply a lot of work, because the server runs on a mobile device(WINCE5.0). This why the most reasonable solution will be to create a new thread on the socket 843. –  Sep 28 '11 at 09:37