Results 1 to 9 of 9

Thread: Flash chat + PHP + Apache = problem

  1. #1

    Flash chat + PHP + Apache = problem

    So I'm trying to make a flash chat client with Obi Admin's tutorial seen here:

    http://www.kirupa.com/developer/flas...ets_flash8.htm

    ... but I've run into a problem. I'm using WAMP server, which is Apache, PHP, and mySQL all bundled together by the way. But anyways, when I try to run the .bat file used in his tutorial, I get an error that closes command prompt. I was able to screenshot the message and it says something about s a fatal error caused by a socket being created on line 45.



    PHP Code:
    #!/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.
    */


    // E_ALL is a named constant (bit of 2047)
    // error_reporting(E_ALL) ensures any errors will be displayed
    error_reporting(E_ALL);

    // set_time_limit() is set to 0 to impose no time limit
    set_time_limit(0);

    // ob_implicit_flush() will push the whole message to flash (including any extra markups like terminating zeros)
    ob_implicit_flush();

    // address set to internal port for the socket to bind to and port set to something above 1024 so flash will connect
    $address '192.168.1.126';
    $port 3306;

    //---- Function to Send out Messages to Everyone Connected ----------------------------------------
    // $allclient is an array of resource id's that php will refer to for the sockets
    // $socket contains the resource id for the flash client who sent the message
    // $buf is just the message they sent, we are sending to everyone using a foreach loop
    function send_Message($allclient$socket$buf) {
       foreach(
    $allclient as $client) {
               
            
    // socket_write uses the $client resource id (which calls on the socket) and sends it our message (which is the second flag)
           
    socket_write($client"$socket wrote: $buf");
       }
    }



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

    // socket_create function is pretty simple, depending on what you want it to do, you can just rig it for that,
    // AF_INET is a "kind of" default address/protocol family that I have used,
    // then socket_create takes and uses SOCK_STREAM, another default available socket type, 
    // and uses a SOL_TCP protocol to have a guaranteed data sending. 
    if (($master socket_create(AF_INETSOCK_STREAMSOL_TCP)) < 0) {
       echo 
    "socket_create() failed, reason: " socket_strerror($master) . "\n";
    }

    // this little beauty is hard to understand, but very useful to our kind of chat
    // we want to be able to reuse our sockets, so we set our $master socket with a level at SOL_SOCKET, 
    // with it reusable (SO_REUSEADDR), and a "1" in the mixed optval spot to make it true.
    socket_set_option($masterSOL_SOCKET,SO_REUSEADDR1);

    // now we need to bind our socket to the address and port specified.
    if (($ret socket_bind($master$address$port)) < 0) {
       echo 
    "socket_bind() failed, reason: " socket_strerror($ret) . "\n";
    }

    // to allow our socket server to listen for incoming calls made by our flash client, we need
    // to use socket_listen on our $master socket. The next flag indicates that only 5 calls at a time.
    // any others will be refused until there is sufficient room to process a new call.
    if (($ret socket_listen($master5)) < 0) {
       echo 
    "socket_listen() failed, reason: " socket_strerror($ret) . "\n";
    }


    // this will allow an easy way to keep track of sockets connected to our server
    $read_sockets = array($master);

    //---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
    while (true) {
       
    $changed_sockets $read_sockets;
       
       
    // socket_select is used here to see if there were any changes with the sockets that were connected before
       // $num_changed_sockets is here so you can check to see if the change is true or not with a subsequent function
       
    $num_changed_sockets socket_select($changed_sockets$write NULL$except NULLNULL);
       
       
    // we run a foreach function on $changed_sockets to see whether it needs to be added (if new), determine the message it sent
       // or determine if there was a socket disconnect
       
    foreach($changed_sockets as $socket) {
               
            
    // now if the $socket currently being checked is the $master socket, we need to run some checks
            // if not then we will skip down to else and check the messages that were sent
           
    if ($socket == $master) {
                   
                
    // socket_accept will accept any incoming connections on $master, and if true, it will return a resource id
                // which we have set to $client. If this did not work then return an error, but if it worked, then add in 
                // $client to our $read_sockets array at the end.
               
    if (($client socket_accept($master)) < 0) {
                   echo 
    "socket_accept() failed: reason: " socket_strerror($msgsock) . "\n";
                   continue;
               } else {
                   
    array_push($read_sockets$client);
               }
           } else {
           
                   
    // socket_recv just receives data from $socket as $buffer, with an integer length of 2048, and a mystery flag set to 0
                // that mystery flag has no real documentation so setting it to 0 will solve it as others have done.
                // we've also set it as a var $bytes in case we need to ensure data sending with socket_write, which is optional
               
    $bytes socket_recv($socket$buffer20480);
               
               
    // if the $bytes we have is 0, then it is a disconnect message from the socket
               // we will just search for it as an index and unset that socket from our $read_sockets and
               // finish up with using socket_close to ensure it is closed off
               
    if ($bytes == 0) {
                   
    $index array_search($socket$read_sockets);
                   unset(
    $read_sockets[$index]);
                   
    socket_close($socket);
               }else{
                    
                    
    // we need to make sure $read_sockets isn't messed with, so setting up a new variable called $allclients
                    // will ensure this. We then shift the array so that our $master socket is not included to the count when
                    // we send our data to all the other sockets in $allclients.
                      
    $allclients $read_sockets;
                       
    array_shift($allclients);
                    
                    
    // just a simple call on our premade function 'send_Message' with $allclients, $socket (current one), and $buffer (the message)
                       
    send_Message($allclients$socket$buffer);
                }
           }
          
       }
    }

    ?>

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

    Also, if needed, this is the actionscript:

    Code:
    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(null, 3036);
     
    //--- Handle button click --------------------------------------
     
    function msgGO() {
    if (inputMsg.htmlText != "") {
    mySocket.send(inputMsg.htmlText+"\n");
    inputMsg.htmlText = "";
    }
    }
     
    pushMsg.onRelease = function() {
    msgGO();
    };
    I'm pretty sure for ip/ports and everything are set up correctly in the .bat and the .swf. According to my server variables, the port is 3036.

    I really don't have a clue on how to tackle this, as I have little PHP experience (just started the other day). A helpful hint in the right direction, or a discovered error that would help narrow it down would receive my deepest thanks.
    Last edited by Kaitenshin; July 24th, 2009 at 10:17 PM.

  2. #2
    the error you took a screenshot of is telling you that the function socket_create() doesn't exhist.

    I would check all the code you are using and try to locate this function. If you can't find it then that probably means that it is a function built into the server that the person who wrote the tutorial uses that WAMP doesn't support.. if thats the case your out of luck.

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  3. #3
    Tested... works on php 5.2.0, im guessing its a customized php installation of WAMP, if you have time, try it with XAMPP and let us know if that worked

    Also, i noticed that you are using port number 3306, wich happens to be the default port of MySQL

    You can never underestimate the stupidity of the general public.

  4. #4
    Quote Originally Posted by RvGaTe View Post
    Tested... works on php 5.2.0, im guessing its a customized php installation of WAMP, if you have time, try it with XAMPP and let us know if that worked

    Also, i noticed that you are using port number 3306, wich happens to be the default port of MySQL
    Very cool. Installing XAMPP now. Can you tell me exactly what you did to get it working (edited which lines of code, how you figured out which port to use, and etc)? Thanks RvGaTe!

  5. #5
    only adjusted the port number, since it had a conflict on my mysql service... other then that, its all the same code you posted.

    You can never underestimate the stupidity of the general public.

  6. #6
    Quote Originally Posted by RvGaTe View Post
    only adjusted the port number, since it had a conflict on my mysql service... other then that, its all the same code you posted.
    What port number did you use? Some random port number, or a specific one?

  7. #7
    Quote Originally Posted by Kaitenshin View Post
    What port number did you use? Some random port number, or a specific one?
    random BB port (button bash port)

    You can never underestimate the stupidity of the general public.

  8. #8
    Quote Originally Posted by RvGaTe View Post
    random BB port (button bash port)
    Ok, so I got the bat file working (it doesn't close). But I must be doing something wrong, because there is still a connection error.

    Think you could send me your php and your bat file?

    I'm soo close.....

    EDIT: So now it is says its connected when I play it in flash, or open it in my path... but not if I try to access it from my browser. I've heard of other people having similar problems (socket policy file server?), but since your success, that might not be the case. I can taste victory... its so close.
    Last edited by Kaitenshin; July 25th, 2009 at 07:44 AM.

  9. #9
    I'm pretty sure this is gone to a client issue now

    http://livedocs.adobe.com/flash/9.0/...licyFile%28%29
    Last edited by RvGaTe; July 25th, 2009 at 09:10 PM. Reason: link wasn't working properly

    You can never underestimate the stupidity of the general public.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012