The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 6 of 6

Thread: help need in php socket and xml socket connection...

  1. #1

    SOLVED:help need in php socket and xml socket connection...

    hi...
    i m building a multiplayer game in flash(as2).
    i made xml socket and php socket.
    now i dont know what to do next, means i dont know how to communicate the socket of xml and php, i read somewhere about the batch files but i wasnt able to grasp that thing.
    if possible plz provide help in details, how to write batch file, what content should i write in batch file, how to run it in my web server...
    thanks in advance...
    ---------------------------
    problem is solved......plz chck the code gvn..
    Last edited by yagnesh; July 28th, 2009 at 08:02 AM. Reason: solved

  2. #2
    Quote Originally Posted by yagnesh View Post
    hi...
    i m building a multiplayer game in flash(as2).
    i made xml socket and php socket.
    now i dont know what to do next, means i dont know how to communicate the socket of xml and php, i read somewhere about the batch files but i wasnt able to grasp that thing.
    if possible plz provide help in details, how to write batch file, what content should i write in batch file, how to run it in my web server...
    thanks in advance...
    I would like to know too, since my problem is similar.

  3. #3
    Quote Originally Posted by yagnesh View Post
    hi...
    i m building a multiplayer game in flash(as2).
    i made xml socket and php socket.
    now i dont know what to do next, means i dont know how to communicate the socket of xml and php, i read somewhere about the batch files but i wasnt able to grasp that thing.
    if possible plz provide help in details, how to write batch file, what content should i write in batch file, how to run it in my web server...
    thanks in advance...
    To get you understand the concept a bit better:

    The socket is used to communicatie... the xml is the language to communicatie with client and server... so its an xml socket, created with php... (or a php socket, with xml communication)

    This socket runs on the server in an endless loop and waits for clients to connect to it, this could be any client, as long as it speaking the same language (xml)

    So the next logical step after creating a xml socket, is to create the client. most people do this with flash on this forum, but again, this could be anything that supports connecting to a socket.

    for all the details and steps involved, try the tutorial http://www.kirupa.com/developer/flas...ets_flash8.htm

    You can never underestimate the stupidity of the general public.

  4. #4
    hey thnx for ur help RVGATE!!!...
    i read tht tutorial in deatil, but now i m facing the problm in runing that socketrun.bat file.
    it gives error "call to undefined function socket_create()"
    how to resolve this problem??
    i m using EASYPHP 3.0 server with APACHE and PHP 5,
    my system is WINDOWS VISTA.

  5. #5
    got the solution!!!!!!!!!!!!!!!!!.........
    just copy the below given php code in your php file:
    ---------------------------------------------------------
    #!/usr/bin/php -q
    <?php

    error_reporting(E_ALL);

    set_time_limit(0);

    ob_implicit_flush();

    echo "Enter Server IP:\n";
    $address = trim(fgets(STDIN));
    //$address = '127.0.0.1';
    echo "Enter Port:\n";
    $port = trim(fgets(STDIN));
    //$port = 4567;
    echo "Attempting to connect to $address:$port\n";

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

    function send_Message($allclient, $buf) {
    global $client_list;
    foreach($allclient as $client) {
    if($client_list[$client]['state'] && $client_list[$client]['nick'] != ""){
    socket_write($client, trim($buf).chr(0));
    }
    }
    }

    function who($allclient, $socket) {
    global $client_list;
    $buf = "";
    $counter = 0;
    foreach($allclient as $client) {
    $buf.=$client_list[$client]['nick'].", ";
    $counter++;
    }
    socket_write($socket, "There are $counter people in this room: $buf".chr(0));
    }

    function send_Single($socket, $buf) {
    socket_write($socket, $buf.chr(0));
    }

    function shutDown($allclients, $master){
    global $abort;
    $abort = false;
    foreach($allclients as $client){
    echo "$client connection closed\n";
    socket_close($client);
    }
    echo "$master connection closed\n";
    socket_close($master);
    echo "Server shutdown complete\n";
    }

    //---- 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) . "\n";
    }else{
    echo "$master socket created\n";
    }

    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) . "\n";
    }else{
    echo "$ret socket bound to $address:$port\n";
    }


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



    $read_sockets = array($master);
    $client_list = array($master);
    $abort = true;
    $policy_file =
    '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'.
    '<cross-domain-policy xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd">'.
    '<allow-access-from domain="*" to-ports="*" secure="false" />'.
    '<site-control permitted-cross-domain-policies="master-only" />'.
    '</cross-domain-policy>';

    //---- Create Persistent Loop to continuously handle incoming socket messages ---------------------
    while ($abort) {
    $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) . "\n";
    continue;
    } else {
    echo "[connection]:$client\n";
    array_push($read_sockets, $client);
    $client_list[$client]['state'] = false;
    $client_list[$client]['nick'] = "";
    send_Single($client, $policy_file);
    send_Single($client, "<b>Enter a nickname:<b>");
    }
    } else {

    $bytes = socket_recv($socket, $buffer, 2048, 0);

    if ($bytes == 0) {
    $nick = $client_list[$socket]['nick'];
    $iindex = array_search($socket, $client_list);
    unset($client_list[$iindex]);
    $index = array_search($socket, $read_sockets);
    unset($read_sockets[$index]);
    $allclients = $read_sockets;
    array_shift($allclients);
    if($client_list[$socket]['nick'] != "" && $client_list[$socket]['nick'] != "<policy-file-request/>"){
    echo "[connection-terminated]:$socket\n";
    send_Message($allclients, "$nick has left the room");
    }
    socket_close($socket);
    }else{
    if($bytes){
    if($client_list[$socket]['state'] === false){
    $tempBuf = trim($buffer);
    $testCase = false;
    foreach($read_sockets as $clients){
    if ($client_list[$clients]['nick'] == $tempBuf) {
    $testCase = true;
    send_Single($socket, "Sorry, \"$tempBuf\" is already in use!");
    send_Single($socket, "Please choose another nickname:");
    break;
    }
    }
    if(!$testCase){
    $client_list[$socket]['nick'] = $tempBuf;
    echo "$tempBuf assigned to $socket\n";
    send_Single($socket, "Hello $tempBuf! Welcome to the game!");
    $allclients = $read_sockets;
    array_shift($allclients);
    who($allclients, $socket);
    if($client_list[$socket]['nick'] != "" && $tempBuf != "<policy-file-request/>"){
    send_Message($allclients, $client_list[$socket]['nick']." has entered the game.");
    }
    $client_list[$socket]['state'] = true;
    }
    }else{
    $allclients = $read_sockets;
    array_shift($allclients);
    if(trim($buffer) == "shut-down-server"){
    shutDown($allclients, $master);
    }else{
    if(trim($buffer) == "/who"){
    who($allclients, $socket);
    }else{
    send_Message($allclients, $client_list[$socket]['nick']." wrote: ".$buffer);
    }
    }
    }
    }
    }
    }
    }
    }

    ?>
    -----------------------------------------
    this will defntly help u. aftr copyin this run ur batch file and thn it vl ask u to entr ur ip n port number enter the same tht u given in fla n php file.
    after ths jst run ur .html file in brwsr
    VIOLAAAAAAAAAAAAAAA!!!!!!!!!!!!!....

  6. #6
    if u r developing sum othr multiplayer application othr than chat thn just add
    $policy_file =
    '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'.
    '<cross-domain-policy xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd">'.
    '<allow-access-from domain="*" to-ports="*" secure="false" />'.
    '<site-control permitted-cross-domain-policies="master-only" />'.
    '</cross-domain-policy>';
    --------------------------
    and
    ------------------
    socket_write($socket, $policy_file.chr(0)); // instead of calling send_single() function for policy file.
    --------------------
    and
    ---------------------
    change $buf to $buf.chr(0);// this will send the whole string means wothevr the contents in $buf, just using $buf will only send the content before the space in the string..
    ------------
    ......plz lemme me know if ther is som problm..

Thread Information

Users Browsing this Thread

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

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