PDA

View Full Version : policy file request when opening socket


yazan
12-21-2008, 04:21 PM
hi all
this is my first post and i hope i get answer :o
i am using a swf file to open socket to some url in a web application, sometimes it connect to the server listening socket then i send my data and get response, but sometimes i am getting nothing and still waiting without response, i used socketsniffer and found that i am getting

<policy-file-request/>
cross-domain-policy><allow-access-from domain="*" to-ports="9000" /><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>...

i read a lot about security policy files, but i do not how to use it.
thanks in advance :D.

yazan
12-22-2008, 05:43 AM
please anyone? am i not clear?

qinn_trinh
12-22-2008, 08:52 AM
this example is grabbed from the help.
in order for socketting to work the server side must reply with a policy file

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="XMLSocketExample()">
<mx:Script>
<![CDATA[

import flash.display.Sprite;
import flash.events.*;
import flash.net.XMLSocket;


private var hostName:String = "localhost";
private var port:uint = 843;
private var socket:XMLSocket;

public function XMLSocketExample():void
{
socket = new XMLSocket();
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(DataEvent.DATA, dataHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(ProgressEvent.PROGRESS, progressHandler);
socket.addEventListener(SecurityErrorEvent.SECURIT Y_ERROR, securityErrorHandler);
addEventListener(ProgressEvent.SOCKET_DATA, onResponse);

socket.connect( hostName, port );
send("hello world!")
}

private function onResponse(event:ProgressEvent):void
{
trace("onResponse: " + event);
}

public function send(data:Object):void
{
socket.send(data);
}

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

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

private function dataHandler(event:DataEvent):void
{
trace("dataHandler: " + event);
ta.text = event.data;
}

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

private function progressHandler(event:ProgressEvent):void
{
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}

private function securityErrorHandler(event:SecurityErrorEvent):voi d
{
trace("securityErrorHandler: " + event);
}

]]>
</mx:Script>
<mx:Button click="send('answer me\n')" >

</mx:Button>
<mx:TextArea id="ta" x="53" y="55"/>
</mx:Application>


and here is a simple ruby script that talks with my as socket, not going to explain it to much, google on policy files there is someone that has already written a policy file server

#ruby

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 843, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client_fd, client_sockaddr = socket.sysaccept
client_socket = Socket.for_fd( client_fd )


pf = "<?xml version=\"1.0\"?>
<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">
<cross-domain-policy>
<site-control permitted-cross-domain-policies=\"master-only\"/>
<allow-access-from domain=\"localhost\" to-ports=\"2929,843\" />
</cross-domain-policy>\0"

puts "The client said #{ client_socket.readline.chomp }"
puts (client_socket.write pf )
puts "The client said #{ client_socket.readline.chomp }"
puts ( client_socket.write "hello\0" )

socket.close

yazan
12-22-2008, 10:51 AM
thanks qinn_trinh for the reply, but i am using flash cs3 not flex, i know we need to use loadPolicyFile function to request the policy file.
what if we did not do this? would the socket connect to the server?
i did this but most of times i tried that there is no any connection to the server, and sometimes it connects for few seconds then the socket will be colosed.
warm regards.

qinn_trinh
12-23-2008, 07:25 AM
Well yes it does connect, and the first thing it asks for is give me a policy file. If it doesn't get one it disconnects...

Anyhow, here's the link to the fellow that wrote a policy file server.

http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html

Try it, and good luck.

yazan
12-23-2008, 09:10 AM
thanks very much for the information.

iDave
12-23-2008, 11:18 AM
I'm experimenting with sockets for the first time (with AIR), I'm trying to connect to gmail to retrieve mail, but I get a "Security sandbox violation". But as far I understood, this happens because I have to use policy files, but in my case (in which I'm connecting to a site which is not under my control), how can I reach my goal?