The Little Guy Posted November 15, 2008 Share Posted November 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/ Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 http://97.91.103.81:6666/ loads fine for me. When I add the chatBox.php, it doesn't. It times out. If I had to guess why it works in Flash, I would guess that the connect method of sockets probably does not automatically handle HTTP stuff, so it's not actually getting that webpage. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690607 Share on other sites More sharing options...
The Little Guy Posted November 15, 2008 Author Share Posted November 15, 2008 chatBox.php is an endless loop, because all it does is wait for connections. the site I got it from does it like this: mySocket.connect("dyn.obi-graphics.com", 9999); Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690618 Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 Uhmmm.... Accessing a PHP page that makes listening sockets through HTTP doesn't make much sense. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690827 Share on other sites More sharing options...
The Little Guy Posted November 15, 2008 Author Share Posted November 15, 2008 Uhmmm.... Accessing a PHP page that makes listening sockets through HTTP doesn't make much sense. Why? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690834 Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690840 Share on other sites More sharing options...
The Little Guy Posted November 15, 2008 Author Share Posted November 15, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690887 Share on other sites More sharing options...
corbin Posted November 15, 2008 Share Posted November 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690952 Share on other sites More sharing options...
The Little Guy Posted November 15, 2008 Author Share Posted November 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-690972 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691124 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 I do run the php through the command line. I have also tried using my IP, and now the connection fails in flash... Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691129 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 I have tried changing the port the socket runs on to port 9999, then I tried http://canyouseeme.org and put in the port, and it doesn't find it. Even when the socket is running. -- Edit -- the port is forwarded on the router, and the firewall is accepting it. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691132 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691140 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 When you exported it to flash, and ran it did you run it from the browser, or from within the flash application its self? I did all those things, and nothing is working for me, I can only connect to the php file from the adobe flash software, and not the web. Mine: http://97.91.103.81:6666/testChat.html Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691152 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691153 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 I have it forwarded to the port, and I have my firewall off, and it still doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691155 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 Obviously something isn't opened, or it would work ;p. Does it work via localhost? If it works via localhost, then it's a forwarding/firewall issue. If it doesn't work via localhost, it's a problem with actually running the script. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691157 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 When you mean localhost do you mean browser or code? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691159 Share on other sites More sharing options...
The Little Guy Posted November 16, 2008 Author Share Posted November 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691161 Share on other sites More sharing options...
corbin Posted November 16, 2008 Share Posted November 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691504 Share on other sites More sharing options...
The Little Guy Posted November 17, 2008 Author Share Posted November 17, 2008 I see that AS3 has a new Socket class http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/Socket.html Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691792 Share on other sites More sharing options...
corbin Posted November 17, 2008 Share Posted November 17, 2008 Yeah..... AS3 still has the same security model though I think. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691797 Share on other sites More sharing options...
tomfmason Posted November 17, 2008 Share Posted November 17, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691835 Share on other sites More sharing options...
The Little Guy Posted November 17, 2008 Author Share Posted November 17, 2008 I put that in a file named crossdomain.xml would that be a valid way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691841 Share on other sites More sharing options...
tomfmason Posted November 17, 2008 Share Posted November 17, 2008 I put that in a file named crossdomain.xml would that be a valid way to do it? I don't think that it will work as the very first thing flash will do is request the cross domain policy from the socket server. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/#findComment-691844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.