Query Server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Query Server

Post by scts102 »

Okay...
I am trying to query an application that I developed via php.

Here is what I am confused on:
-The socket on the app is using a command that sends just text. It does not use a file stream or anything like that. Will php accept this?
-I have connected to the application using the following code I modified from a game server query script:

function file:

Code: Select all

<?
function getnull ($src) { 
    $data = ""; 
    $out = ""; 
    while($data != "\0") { 
        $data = fread($src, 1); 
        $out .= $data; 
    } 
    return substr($out, 0, -1); 
}

function hlsend ($udp, $str) { 
    fwrite($udp, "${str}"); 
    fread($udp, 4); 
}
function server($addr){
 if(strstr($addr,"/")){$addr = substr($addr, 0, strpos($addr, "/"));}
 return $addr;
}
list($addr,$port)= explode (':', $server_ip);
 $fp = @fsockopen("tcp://".server($addr), $port, $errno, $errstr);
 if(!$fp) {
  $hl["st"] = 0;
 } else {
  hlsend($fp, "\\version\\"); 
 $testme= fread($fp, 1); 
 // $hl["version"] = getnull($fp); 
 //  $oar = array();
// if (!$fp){
// echo $errstr;
//  $oar["st"] = 0;
// } else {
//  $oar["st"] = 1;
//  $oar["na"] = $hl["version"];
 // $oar["cp"] = $hl["current_players"];
 // $oar["mp"] = $hl["max_players"];
 // $oar["ga"] = $hl["description"];
//  $oar["sv"] = $server_ip;
 // unset($oar);
  }
  fclose($fp);
  ?>
index file:

Code: Select all

<?
// first we make sure that server ip is set:
$server_ip = "24.105.221.206:6000";

// then we include the file required (as long as server ip is set)
if($server_ip == "" || !empty($server_ip)) {
    require("ncss.php");
}

//if($hl["st"] == 0) {
// echo "server is offline";
//} else {
 // do whatever if the server is online
 //echo "server: ".$hl['sv']."<br>";
 echo ($testme);
 // list players:
// if ($hl["cp"] > 0) {
 // for ($i = 0; $i < $hl["cp"]; $i++) {
 //  echo $hp[$i]["name"]." - ";
 //  echo "<b>".$hp[$i]["frags"]."</b> - "; 
 //  echo $hp[$i]["time"]."<br>";
 // }
// } else { 
//   echo "<b>no</b> players online\n"; 
 // }

?>
As you can see, I did a horrendous job of doing this.

Now, due to the logging that I put in the app, I can see that it connects, and that it sends the query command "\version\", but how can I get it to display what is sent? Note that each command will return a simple text value, not an array of values.
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

I figured this out...I needed to change the length of bytes that were being read.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Check out http://dev.kquery.com and their coding forums for some source code for this sort of app. It'll help when designing your own.
Post Reply