View Full Version : Can't connect to socket once published (not even locally)
wokawaka
April 25th, 2008, 11:26 AM
Hey.
I've made a flash program that connects to a java server.
This works perfectly in the IDE ( Flash CS3 ).
When i however try to open the exported .swf from my Harddrive ( same pc as server is runnign on (127.0.0.1) ) the java server does not even recognize any attempt to connect to it.
After reading a while i found out that flash 9 uses policy files
Added this functionality to my server, which now listens for requests on port 843.
This results in same thing as before. Nothing even tries to connect =(
What should i do ?
Jerryscript
April 25th, 2008, 05:41 PM
We would have to see your connection code to help.
magcius
April 25th, 2008, 05:54 PM
Make sure that when you export, you select that you want local access, not network access. I'm not sure how to do this off the top of my head.
EDIT: It's in Publish Settings -> Flash -> Local Playback Security (bottom combobox). Set that to allow the network.
wokawaka
April 26th, 2008, 04:44 AM
tried with
host = "10.1.0.69";
and
host = "localhost";
The server sees no connections at all when im trying to run it outside of IDE.
Am gonna try to run it from a webpage aswell se if that makes any difference..
Heres the connection stuff. Don't laugh to much =P
package AS.socket{
import flash.events.Event;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.display.Sprite;
import AS.encryption.*;
import AS.Events.*
public class socketConnector2 extends Sprite {
private var eve:Events;
private var host:String;
private var port:int;
private var socket:Socket;
private var encryption:rot;
private var connInited:Boolean;
public function socketConnector2(eve:Events) {
this.eve = eve;
host = "10.1.0.69";
port = 9765;
connInited = false;
encryption = new rot(10);
connect();
}
public function sendMessage( message:String ) {
message = message + "\n";
if ( socket && socket.connected ) {
socket.writeUTFBytes( message );
socket.flush();
}
}
private function receiveMessage( message:String ):void {
if (connInited) {
eve.getEventMessage(message);
} else {
trace( "'" + collapseWhiteSpace(message) + "'");
if ( message == "accepted") {
trace("accepted!");
connInited = true;
return;
}
message = encryption.decrypt(message);
var answr:int = parseInt(message)*3); // yeah its stupid, so shoot me =P
sendMessage( encryption.encrypt(""+answr) );
}
}
private function connect():void {
socket = new Socket();
socket.addEventListener(Event.CONNECT, socketConnect);
socket.addEventListener(Event.CLOSE, socketClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, socketError );
try {
socket.connect(host, port);
} catch (e:Error) {trace(e);}
}
private function socketConnect( event:Event ):void {
socket.addEventListener( ProgressEvent.SOCKET_DATA, socketData );
}
private function socketData( event:ProgressEvent ):void {
try {
receiveMessage( socket.readUTFBytes( socket.bytesAvailable ) );
} catch (error:Error) {trace(e);}
}
private function socketClose( event:Event ):void {
trace( "Socket Closed" );
}
private function socketError( event:IOErrorEvent ):void {
trace( "Socket has run into an Error" );
}
// function just used to show data in a nice way.. remove when done.
function collapseWhiteSpace( theString ) {
theString = theString.split("\r").join("");
theString = theString.split("\t").join("");
while ( theString.indexOf(" " ) != -1 ) {
theString= theString.split(" ").join(" ");
}
if (theString.substr(0,1) == " ") {
theString = theString.substr( 1 );
}
if (theString.substr( theString.length-1, 1 ) == " ") {
theString = theString.substr( 0, theString.length - 1 );
}
return theString;
}
}
}
wokawaka
April 27th, 2008, 02:59 PM
did you understand what was wrong?
Jerryscript
April 27th, 2008, 03:42 PM
When you use this class, consider the Flash Player security model:
Data loading is not allowed if the calling SWF file is in the local-with-file-system sandbox and the target resource is from a network sandbox.
Data loading is also not allowed if the calling SWF file is from a network sandbox and the target resource is local.
The calling SWF file and the network resource being accessed must be in exactly the same domain. For example, a SWF file at adobe.com can connect only to a server daemon at adobe.com.
Websites can permit cross-domain access to a resource through a cross-domain policy filePerhaps the issue is with local security?
wokawaka
April 27th, 2008, 06:08 PM
Is there a way to make the IDE act just as if it was on a real webpage?
So the permissions request is still made even though its in the Flash CS3 program.
Jerryscript
April 28th, 2008, 10:42 AM
Not that I know of, though you may be able to do a work-around using the flash.System methods. It wouldn't create the exact conditions, but you could possibly fake them.
Best bet, test in the environment you are publishing for (webpage, desktop exe, etc).
wokawaka
May 2nd, 2008, 02:50 PM
Hey.
Im still having this problem after the .swf has been published.
If im on the same network as my server it will connect. That is if im inside the same network.
The server is my networks router and therfore has two ethernet ports connected.
One is LAN and servers ip addresses 10.1.0.0 and one is wan and has a static ip 213.113.*.* (im not posting my ip publically)
The flash socket error event returns:
Socket has run into an Error
[IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2031: Socket Error. URL: 213.113.*.*"]
And i am 100% sure that the ip is right. It does not work if i use the servers domain name ( a .com address ) either
So like i said it works perfectly on local (using 10.1.0.1), but not from outside.
I do not get a request for any security files either. Im monitoring all incomming traffic to the server it does not even get a connection request.
Jerryscript
May 2nd, 2008, 07:14 PM
Have you forwarded the appropriate ports on your router?
wokawaka
May 3rd, 2008, 04:50 PM
Have you forwarded the appropriate ports on your router?Yeah, thats what i though it was first to, a pc plays router and is listening to the proper ports and has the program running, ive checked the setup now several times and im sure its right.
I can connect from within the IDE with no problems from my laptop when inside the LAN, but not not when trying from a browser (from local harddrive) i really dont understand it.
Everything works perfectly ( except there is no xml socket persmission request ever ) from the IDE, but nothing works outside where i get no requests of any kind at all.
Is there any type of information i could check or try to get to make the problem clearer?
Jerryscript
May 4th, 2008, 12:38 AM
Check the adobe security tech notes. Sounds like it's a security sandbox issue, since swf on the harddrive are in a very strict sandbox.
wokawaka
May 7th, 2008, 08:31 AM
Ok, ive managed to connect.
As you said before the sandbox on the harddrive was very restrictive. When i placed the .swf on the web it would perform as expected.
Now im stuck on a new problem however.
Flash sends the '<policy-file-request/>' to my policy socket server listening at port 843 and i respond with the following in one line
<?xml version="1.0"?><allow-access-from domain="befen.se" to-ports="my-port-number" /></cross-domain-policy>
(where my-port-number is actually the port im using and not text)
I respond using the following java code where 'policyFile' is the string containing the above line, and 'cli' is the socket the .swf file connected on
PrintWriter out = new PrintWriter(cli.getOutputStream());
out.println(policyFile);
This however results in the .swf file dissconnecting and trying the real port that it is later supposed to connect to and doing the same policy file request.
Which offcourse also ends in dissconnect since i dont want it to go there and do not respond to that request in any way.
Now what am i doing wrong here. it sends the request i respond to it, it still disconnects..
Thanks
//Woka
wokawaka
May 7th, 2008, 08:55 AM
got it to work with
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="befen.se" to-ports="122343123" />
</cross-domain-policy>
and
PrintWriter out = new PrintWriter(cli.getOutputStream());
out.print(policyFile);
out.flush();
maybe someone else will have use of it..
Jerryscript
May 7th, 2008, 10:23 AM
Glad you got it working, these new security policy files can be a headache. For anyone wanting to read up more on how it works, here's a direct link:
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.