Jump to content

flash connecect()


The Little Guy

Recommended Posts

I wrote a PHP chat server, that works fine. I can connect to it from within Flash CS3 but when I go to http://localhost:6666/testChat.html it can't connect to the php server, why? I have tried it on a the same computer, and a different computer.

 

I opened both ports 6666 (web server), and 6667 (php socket server) in my firewall, and I did port forwarding in my router for both, I just don't know why it works in Adobe Flash, but not on the web server.

 

flash connect code:

mySocet.connect("97.91.103.81:6666/chatBox.php"); // Connect to the socket server

Link to comment
Share on other sites

Well HTTP is not equivalent to a bare socket (or another protocol).

 

 

I really don't feel like explaining the concept of sockets.....  Bleh.  It just doesn't make sense.  What you'll want to do is run the PHP script such that it's listening on port X, and then you'll want to connect to host:X.

Link to comment
Share on other sites

Here is what I want to do... Maybe I am doing this all wrong.

 

OK, I have my page located here: http://97.91.103.81:6666/testChat.html It contains my SWF file.

 

Next I want to connect to my chat server written with the sockets. So I thought I had to place that file in the web root directory, but maybe I am wrong... Do I or should I place it in a different directory outside the web root?

Link to comment
Share on other sites

That's just Apache listening on 6666, right?

 

 

If so, then going to the PHP page and trying to write back and forth to it isn't going to work.

 

 

 

The PHP script is creating a listening socket, yes?  Somewhere there is a socket_create() call?

 

 

If so, then your other stuff needs to connect to it on what ever port it is that it's listening on.

Link to comment
Share on other sites

Apache is listening on port 6666 is correct

 

The PHP:

#!/usr/bin/php -q
<?php
/*
Raymond Fain
Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
For any questions or concerns, email me at ray@obi-graphics.com
or simply visit the site, www.php.net, to see if you can find an answer.
*/


error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
//$address = '192.168.1.75';
$address = 'localhost';
$port = 1213;

//---- Function to Send out Messages to Everyone Connected ----------------------------------------

function send_Message($allclient, $socket, $buf) {
   foreach($allclient as $client) {
      socket_write($client, "$client talk: $buf");
   }
}

//---- Start Socket creation for PHP 5 Socket Server -------------------------------------

if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
   echo "socket_create() failed, reason: " . socket_strerror($master) . " ";
}

socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
   echo "socket_bind() failed, reason: " . socket_strerror($ret) . " ";
}

if (($ret = socket_listen($master, 5)) < 0) {
   echo "socket_listen() failed, reason: " . socket_strerror($ret) . " ";
}

$read_sockets = array($master);

//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
   $changed_sockets = $read_sockets;
   $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
   foreach($changed_sockets as $socket) {
      if ($socket == $master) {
         if (($client = socket_accept($master)) < 0) {
            echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " ";
            continue;
         } else {
            array_push($read_sockets, $client);
         }
      } else {
         $bytes = socket_recv($socket, $buffer, 2048, 0);
         if ($bytes == 0) {
            $index = array_search($socket, $read_sockets);
            unset($read_sockets[$index]);
            socket_shutdown($socket,2);
            socket_close($socket);
         }else{
            $allclients = $read_sockets;
            array_shift($allclients);
            send_Message($allclients, $socket, $buffer);
         }
      }
   }
}
?>

 

The Flash:

mySocket  =  new XMLSocket();
mySocket.onConnect = function(success) {
   if (success) {
      msgArea.htmlText += "<b>Server connection established!</b>";
   } else {
      msgArea.htmlText += "<b>Server connection failed!</b>";
   }
}
mySocket.onClose = function() {
   msgArea.htmlText += "<b>Server connection lost</b>";
}
XMLSocket.prototype.onData = function(msg) {
   msgArea.htmlText += msg;
}
mySocket.connect('localhost', 1213);
//--- Handle button click --------------------------------------
function msgGO() {
   if (inputMsg.htmlText != "") {
      mySocket.send(inputMsg.htmlText);
      inputMsg.htmlText = "";
   }
}
pushMsg.onRelease = function() {
   msgGO();
}
keyListener = new Object();
keyListener.onKeyDown = function(){
   xk = parseInt(Key.getAscii().toString());
   if(xk == 13){
      msgGO();
   }
}
Key.addListener(keyListener);

 

The tutorial can be found here: http://www.kirupa.com/developer/flash8/php5sockets_flash8.htm

Link to comment
Share on other sites

Try running the PHP script through the command line.

 

 

 

Edit:  If you want someone to be able to connect to the chat server from the outside through that Flash applet, you will have to make it say your IP address or host name and open your firewall, by the way.

Link to comment
Share on other sites

lol you're way over complicating this.... I just did the following and it worked fine for me:

 

 

Downloaded the ZIP at the end of that tutorial.

Ran the PHP file.  (php socketTut.php)

Opened the Flash file and changed the host to localhost.

Exported the Flash file and opened it.

 

 

And it connected fine.

 

 

 

I'm not sure what you're doing wrong lol.  Also, try to get it working locally using localhost first before you try to port forward and what not.

Link to comment
Share on other sites

Ohhhh.....  You're problem is actually firewall related.  By accessing it from your WAN ip, it's trying to go through your firewall.  Setup your router to forward what ever port, and make sure your firewall is open on that port.

 

 

telnet 97.91.103.81 <port> should work.

Link to comment
Share on other sites

well... Here is my EXACT CODE

 

PHP SOCKET:

#!/usr/bin/php -q
<?php
/*
Raymond Fain
Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com
For any questions or concerns, email me at [email]ray@obi-graphics.com[/email]
or simply visit the site, [url=http://www.php.net]www.php.net[/url], to see if you can find an answer.
*/


error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
//$address = '192.168.1.75';
//$address = 'localhost';
$address = '127.0.0.1';
$port = 45063;

//---- Function to Send out Messages to Everyone Connected ----------------------------------------

function send_Message($allclient, $socket, $buf) {
   foreach($allclient as $client) {
      socket_write($client, "$client talk: $buf");
   }
}

//---- Start Socket creation for PHP 5 Socket Server -------------------------------------

if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
   echo "socket_create() failed, reason: " . socket_strerror($master) . " ";
}

socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
if (($ret = socket_bind($master, $address, $port)) < 0) {
   echo "socket_bind() failed, reason: " . socket_strerror($ret) . " ";
}

if (($ret = socket_listen($master, 5)) < 0) {
   echo "socket_listen() failed, reason: " . socket_strerror($ret) . " ";
}

$read_sockets = array($master);

//---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
while (true) {
   $changed_sockets = $read_sockets;
   $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
   foreach($changed_sockets as $socket) {
      if ($socket == $master) {
         if (($client = socket_accept($master)) < 0) {
            echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " ";
            continue;
         } else {
            array_push($read_sockets, $client);
         }
      } else {
         $bytes = socket_recv($socket, $buffer, 2048, 0);
         if ($bytes == 0) {
            $index = array_search($socket, $read_sockets);
            unset($read_sockets[$index]);
            socket_shutdown($socket,2);
            socket_close($socket);
         }else{
            $allclients = $read_sockets;
            array_shift($allclients);
            send_Message($allclients, $socket, $buffer);
         }
      }
   }
}
?>

 

FLASH ACTIONSCRIPT:

mySocket  =  new XMLSocket();
//var host = 'localhost';
//var host = '192.168.1.75';
var host = '127.0.0.1';
var port = 45063;
msgArea.htmlText = "Host: "+host;
msgArea.htmlText += "Port: "+port;
mySocket.onConnect = function(success) {
   if (success) {
      msgArea.htmlText += "<b>Server connection established!</b>";
   } else {
      msgArea.htmlText += "<b>Server connection failed!</b>";
   }
}
mySocket.onClose = function() {
   msgArea.htmlText += "<b>Server connection lost</b>";
}
XMLSocket.prototype.onData = function(msg) {
   msgArea.htmlText += msg;
}
mySocket.connect(host, port);
//--- Handle button click --------------------------------------
function msgGO() {
   if (inputMsg.htmlText != "") {
      mySocket.send(inputMsg.htmlText);
      inputMsg.htmlText = "";
   }
}
pushMsg.onRelease = function() {
   msgGO();
}
keyListener = new Object();
keyListener.onKeyDown = function(){
   xk = parseInt(Key.getAscii().toString());
   if(xk == 13){
      msgGO();
   }
}
Key.addListener(keyListener);

 

Using this, When I run it from: http://localhost:6666/testChat.swf it DOES NOT WORK

Link to comment
Share on other sites

Whoa....

 

 

After googling and testing my self for like 10 minutes, I finally found the problem....

 

http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary860.html

 

The XMLSocket.connect method can connect only to computers in the same subdomain where the SWF file (movie) resides. This restriction does not apply to movies running off a local disk. (This restriction is identical to the security rules for loadVariables , XML.sendAndLoad , and XML.load .)

 

 

I've tried every host combination I can think of, and nothing seems to work.

 

We're either doing it entirely wrong or it's a bug.  It must just be something simple that I'm missing.

 

If I host the file on corbin.no-ip.org and make it connect to corbin.no-ip.org, you would think it would work, since I know my ports are all forwarded correctly, but no....  Hrmmm....  Even if I use null as the first arg of the connect method (null tells it to connect to the host that is hosting the Flash movie) it still doesn't work.  Wtf?

Link to comment
Share on other sites

you will also need to address a policy request from flash or your connections will continue to fail. Here is an example from a python xml socket server I wrote using twisted

 

class PushServerProtocol(XmlStream):
def __init__(self):
	self.factory = PushServerFactory()
	self.policyFile = '<cross-domain-policy><allow-access-from domain="*" to-ports="your_app_port" /></cross-domain-policy>'
	self.policyRequest = '<policy-file-request/>'
def connectionMade(self):
	self.id = len(self.factory.clients) + 1
	self.factory.clients.append(self)
def connectionLost(self, reason):
	self.factory.clients.remove(self)
def dataReceived(self, data):
	print 'data received'
	if data.find(self.policyRequest) >= 0:
		print 'policy request'
		self.writeToClient(self.policyFile, self.id)
	else:
		self.handleData(data)

also, the delimiter is \0 so testing data as a literal <policy-file-request/> would fail. When flash requests your policy you will need to respond with <cross-domain-policy><allow-access-from domain="*" to-ports="your_app_port" /></cross-domain-policy>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.