Server Detector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Hiler

    Server Detector

    Hi guys,

    I've written a small bit of PHP code who's purpose is to detect my game
    server. If the game server is running, I want to display the Launcher and
    Client population (two parts of the game program).

    So here is what I wrote, minus the address and port :)

    <?php
    if(($sock = socket_create (AF_INET, SOCK_DGRAM, 0)) < 0)
    {
    echo "socket_create( ) failed: reason: " . socket_strerror ($sock) . "<BR>";
    }
    else
    {
    $server='xxx.xx x.xxx.xxx';
    $port=yyyy;
    $send=sprintf(' %c%c%c%c',24, 0, 0, 0);
    $x=socket_sendt o($sock,$send,s trlen($send)-1,0,$server,$po rt);

    $bar = array($sock);
    $select_result = socket_select($ bar, $b=null, $c=null, 3);
    if ($select_result == true)
    {
    $y=socket_recvf rom($sock,&$rec vbuf,1024,0,&$f rom,&$port);
    $LauncherPopula tion = sprintf('%d', $recvbuf[0]);
    $ClientPopulati on = sprintf('%d', $recvbuf[4]);
    echo "<CENTER>Th e Sovereignty Server<BR>";
    echo "is UP</CENTER><BR>";
    ?>
    <CENTER><img src="GreenGem.p ng" border="0"></CENTER>
    <?php
    echo $recvbuf . $y . "<BR>";
    echo "Launcher Population:" . $LauncherPopula tion . "<BR>";
    echo "Client Population:" . $ClientPopulati on . "<BR><BR>";
    }
    else
    {
    echo "<CENTER>Th e Sovereignty Server<BR>";
    echo "is DOWN</CENTER><BR>";
    ?>
    <CENTER><img src="RedGem.png " border="0"></CENTER>
    <?php
    }
    }
    socket_close($s ock);
    ?>

    This works to some extent. If the server is not running, I get a timeout
    after 3 seconds and the red gem graphic is shown. If it is running the
    server responds and I get the green gem graphic. The server sends back two
    integers for the population

    The problem is the $LauncherPopula tion and $ClientPopulati on values. They
    always come up 0 even when there are player in game. The result of the
    socket_recvfrom call ($y) is 8, which is the proper size for two integers,
    and I know for sure that the server is sending the correct values. But for
    some reason, my buffer doesn't appear to be set with them.

    What am I doing wrong?

    Ron
    --
    Creation is an act of sheer will

    Home of "Manifest Destiny" and "Sovereignt y"


  • Chung Leong

    #2
    Re: Server Detector

    What do you mean exactly that you said "The server sends back two
    integers?" Is the server sending two integers printed out as text ("45
    64") or two integers in binary? If it's sending back two binary 32 bit
    numbers, then you need to use unpack:

    $a = unpack("Nlp/Ncp", $recvbuf);
    $LauncherPopula tion = $a['lp'];
    $ClientPopulati on = $a['cp'];

    I'm assuming big-endian packing order here, since you said you're
    getting zero from recvbuf[0] and recvbuf[4].

    If the numbers are returned in a text string, then you need to extract
    them using sscanf(), preg_match() or whatever.

    "Ron Hiler" <rhiler@spam.be .gone.rjcyberwa re.com> wrote in message news:<3fc91acf$ 1@wobble>...[color=blue]
    > Hi guys,
    >
    > I've written a small bit of PHP code who's purpose is to detect my game
    > server. If the game server is running, I want to display the Launcher and
    > Client population (two parts of the game program).
    >
    > So here is what I wrote, minus the address and port :)
    >
    > <?php
    > if(($sock = socket_create (AF_INET, SOCK_DGRAM, 0)) < 0)
    > {
    > echo "socket_create( ) failed: reason: " . socket_strerror ($sock) . "<BR>";
    > }
    > else
    > {
    > $server='xxx.xx x.xxx.xxx';
    > $port=yyyy;
    > $send=sprintf(' %c%c%c%c',24, 0, 0, 0);
    > $x=socket_sendt o($sock,$send,s trlen($send)-1,0,$server,$po rt);
    >
    > $bar = array($sock);
    > $select_result = socket_select($ bar, $b=null, $c=null, 3);
    > if ($select_result == true)
    > {
    > $y=socket_recvf rom($sock,&$rec vbuf,1024,0,&$f rom,&$port);
    > $LauncherPopula tion = sprintf('%d', $recvbuf[0]);
    > $ClientPopulati on = sprintf('%d', $recvbuf[4]);
    > echo "<CENTER>Th e Sovereignty Server<BR>";
    > echo "is UP</CENTER><BR>";
    > ?>
    > <CENTER><img src="GreenGem.p ng" border="0"></CENTER>
    > <?php
    > echo $recvbuf . $y . "<BR>";
    > echo "Launcher Population:" . $LauncherPopula tion . "<BR>";
    > echo "Client Population:" . $ClientPopulati on . "<BR><BR>";
    > }
    > else
    > {
    > echo "<CENTER>Th e Sovereignty Server<BR>";
    > echo "is DOWN</CENTER><BR>";
    > ?>
    > <CENTER><img src="RedGem.png " border="0"></CENTER>
    > <?php
    > }
    > }
    > socket_close($s ock);
    > ?>
    >
    > This works to some extent. If the server is not running, I get a timeout
    > after 3 seconds and the red gem graphic is shown. If it is running the
    > server responds and I get the green gem graphic. The server sends back two
    > integers for the population
    >
    > The problem is the $LauncherPopula tion and $ClientPopulati on values. They
    > always come up 0 even when there are player in game. The result of the
    > socket_recvfrom call ($y) is 8, which is the proper size for two integers,
    > and I know for sure that the server is sending the correct values. But for
    > some reason, my buffer doesn't appear to be set with them.
    >
    > What am I doing wrong?
    >
    > Ron[/color]

    Comment

    • Ron Hiler

      #3
      Re: Server Detector

      Thanks for the response, Chung. I was sending the ints binary, which turned
      out to be the problem.

      I got it, though. My web site server uses a slightly older version of php
      (4.2.3, Jan 8 2003), and apparaently the socket_recvfrom () function doesn't
      support binary data in that version. I changed to using plaintext instead
      of binary, and the values came through fine :)

      Ron
      --
      Creation is an act of sheer will

      Home of "Manifest Destiny" and "Sovereignt y"


      Comment

      Working...