A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Php socket server

  1. #1
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    Php socket server

    Hi, I am trying to use this code here.

    PHP Code:
    #!/usr/local/bin/php -q
    <?php
    //setting this to 0 lets scriphang around for ever
    set_time_limit(0);



    // defaults...

    define('MAXLINE'1024); // how much to read from a socket at a time
    define('LISTENQ'10); // listening queue
    define('PORT'10000); // the default port to run on
    define('FD_SETSIZE'5); // file descriptor set size (max number of concurrent clients)...


    // for kill the server

    function killDaemon()
    {
    global 
    $listenfd$client;

    socket_close($listenfd);
    $msg "Daemon going down!\n";
    for (
    $i 0$i FD_SETSIZE$i++)
    {
    if (
    $client[$i] != null)
    {
    socket_write($client[$i], $msgstrlen($msg));
    socket_close($client[$i]);
    }
    }

    print 
    "Shutting down the daemon\n";
    exit;
    }


    // whenever a client disconnects...

    function closeClient($i)
    {
    global 
    $client$remote_host$remote_port;

    print 
    "closing client[$i] ({$remote_host[$i]}:{$remote_port[$i]})\n";

    socket_close($client[$i]);
    $client[$i] = null;
    unset(
    $remote_host[$i]);
    unset(
    $remote_port[$i]);
    }


    // set up the file descriptors and sockets...

    // $listenfd only listens for a connection, it doesn't handle anything
    // but initial connections, after which the $client array takes over...

    $listenfd socket_create(AF_INETSOCK_STREAM0);
    if (
    $listenfd)
    print 
    "Listening on port " PORT "\n";
    else
    die(
    "AIEE -- socket died!\n");

    //set servers ip here, leave if using a local host
    socket_setopt($listenfdSOL_SOCKETSO_REUSEADDR1);
    if (!
    socket_bind($listenfd"0.0.0.0"PORT))
    {
    socket_close($listenfd);
    die(
    "AIEE -- Couldn't bind!\n");
    }
    socket_listen($listenfdLISTENQ);


    // set up our clients. After listenfd receives a connection,
    // the connection is handed off to a $client[]. $maxi is the
    // set to the highest client being used, which is somewhat
    // unnecessary, but it saves us from checking each and every client
    // if only, say, the first two are being used.

    $maxi = -1;
    for (
    $i 0$i FD_SETSIZE$i++)
    $client[$i] = null;


    // the main loop.

    while(1)
    {
    $rfds[0] = $listenfd;

    for (
    $i 0$i FD_SETSIZE$i++)
    {
    if (
    $client[$i] != null)
    $rfds[$i 1] = $client[$i];
    }


    // block indefinitely until we receive a connection...

    $nready socket_select($rfds$null$nullnull);


    // if we have a new connection, stick it in the $client array,

    if (in_array($listenfd$rfds))
    {
    print 
    "listenfd heard something, setting up new client\n";

    for (
    $i 0$i FD_SETSIZE$i++)
    {
    if (
    $client[$i] == null)
    {
    $client[$i] = socket_accept($listenfd);
    socket_setopt($client[$i], SOL_SOCKETSO_REUSEADDR1);
    socket_getpeername($client[$i], $remote_host[$i], $remote_port[$i]);
    print 
    "Accepted {$remote_host[$i]}:{$remote_port[$i]} as client[$i]\n";
    break;
    }

    if (
    $i == FD_SETSIZE 1)
    {
    trigger_error("too many clients"E_USER_ERROR);
    exit;
    }
    }
    if (
    $i $maxi)
    $maxi $i;

    if (--
    $nready <= 0)
    continue;
    }


    // check the clients for incoming data.

    for ($i 0$i <= $maxi$i++)
    {
    if (
    $client[$i] == null)
    continue;

    if (
    in_array($client[$i], $rfds))
    {
    $n trim(socket_read($client[$i], MAXLINE));

    if (!
    $n)
    closeClient($i);
    else
    {
    // if a client has sent some data, do one of these:

    if ($n == "/killme")
    killDaemon();
    else if (
    $n == "/quit")
    closeClient($i);
    else
    {
    // print something on the server, then echo the incoming
    // data to all of the clients in the $client array.
    //$n is incoming data

    print "From {$remote_host[$i]}:{$remote_port[$i]},>client[$i]: $n\n";
    for (
    $j 0$j <= $maxi$j++)
    {
    if (
    $client[$j] != null)
    socket_write($client[$j], "From client[$i]:>$n".chr(0));
    //the chr(0) send the nul character required by flash
    }
    }
    }

    if (--
    $nready <= 0)
    break;
    }
    }
    }

    ?>

    I loaded the .php onto my webserver (that is a newly set up one, only an index.php file). and when I load it it's a blank page, so I think it's working. I then copy the fla file from the post #37 here http://board.flashkit.com/board/show...=391320&page=2

    but it doesnt seem to connect? Why might that be...

    I am trying port 1000, and in the php file I put the ip adress of the website that i got using http://www.selfseo.com/find_ip_address_of_a_website.php.

    and the flash file is loaded into the same directory on the server aswell as the html that displays the swf. and I also put the same ip adress into the flash file. but no connection. Any ideas?

  2. #2
    Senior Member
    Join Date
    Apr 2009
    Posts
    138

    hmm

    well now i tried to leave the ip blank in flash, and i tried using #!/usr/local/bin/php -q as well, but it doesn't work. I tried to ping the site and use that ip.

    Safe mode is off, and I have all the socket functions available.. I tried some code that said it would bypass the security settings. didn't work..

    I tried deleting the code and renaming between so it would restart.

    also it seems to show this,
    #!/usr/lib/php -q

    or

    #!/usr/local/bin/php -q

    I think I tried another one too, so i am wondering if the path to my php.exe is wrong? Also, I tried to run the same socketserver.php file in another browser at the same time, but nothing interesting happens. The socketserver.php is in my root directory along with the flash and html.. I feel like I have tried everything, yet everyone else says there's is working fine.. I don't get it.. also i am using php5.3

    any help would be greatly appreciated on this reoccurring topic...
    this thing is just driving me nuts :P


    Thanks,
    Brick

  3. #3
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Stupid question, but why PHP and not something like ElectroServer? It's far harder and less effective to use PHP to make a socket server when you could use a purpose-built server designed just to do this - one that comes with many of examples and tutorials on how to write games against it.

    -Mike

  4. #4
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    never tried it, I only read it in one of the posts but didn't go there. Is it one of those servers I need to give credit to in my game though?

    And that is def not a stupid question, I'll look into that. But I am also so frustrated with why it wont work lol! Would love to at least connect!

  5. #5
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    i just noticed 25 max users, or i'd have to pay for more.. that's kinda lame :\

  6. #6
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    The rule of thumb is that a high-quality, popular, multi-player game has between a 10 to 1 to a 100 to 1 "monthly active user" (people who play at least once per month) to concurrent player ratio. That means you would have 250 to 2500 monthly active users before you reach your 25 concurrent user count. Finally, the monthly active user ratio is always much less than your total registered user count. So you might have 10,000 registered players before you even have to consider paying for anything.

    Additionally, the cost is quite inexpensive and you only pay the difference in licenses so you can start small and grow. This is particularly true considering the product has cost us many hundreds of thousands of dollars to develop over the last 7 years, the price is extremely reasonable

    With that said, we are looking to increase the free user count as soon as we get an update to our licensing application complete.

    Ultimately, I'd focus on making a great game before worrying about an inexpensive price tag that you only pay once you are already successful.

    -Mike

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    Enough said :P, Plus id be in the starting stage anyways for a few months. Now you say "us" so I assume you are part of the dev group? Now can I use the app twice to make 2 server rooms that host 25 each, or is that a rule breaking thing. I also wasn't even gonna do the register thing, just a connect and play against the world

  8. #8
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Quote Originally Posted by brickhouse420
    Enough said :P, Plus id be in the starting stage anyways for a few months. Now you say "us" so I assume you are part of the dev group? Now can I use the app twice to make 2 server rooms that host 25 each, or is that a rule breaking thing. I also wasn't even gonna do the register thing, just a connect and play against the world
    Yes, I'm one of the owners of Electrotank - the company that makes ElectroServer. Yes, you can run multiple instances like that no problem.

    -Mike

  9. #9
    Senior Member
    Join Date
    Apr 2009
    Posts
    138
    Possible to make mule tiplayer with out using a server, I have a web host but I cant install actual programs on the server just stuff through ftp.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center