User:Pizzahut/msqp.php: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with 'This is a code example for querying a Half-Life master server using the PHP language. For querying more than 231 servers it needs to be extended. The purpose of this program is t…')
 
(Getting an error when changing the source code, so I put the update on PasteBin.)
Tag: Replaced
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
This is a code example for querying a Half-Life master server using the PHP language. For querying more than 231 servers it needs to be extended. The purpose of this program is to get a list of TFC servers, there are only about 150 left at the time of writing so it doesn't check for more. Btw currently the master servers only return 50 to 70 TFC servers, but as stated there are about 150, which you can verify with HLSW's Gamers Search (when it's not having one of its off days).
This is a code example for querying a Half-Life master server using the PHP language.<br>
<pre>
See also [[User:Pizzahut/test.php]] - a test script in PHP to check which master servers respond.
<?php


// There are two HL1 master list servers:
* https://pastebin.com/D2mjg9Nb
// 1. 69.28.151.162 hl1master.steampowered.com
// 2. 72.165.61.190
// Using the domain name is preferred, but this server often times out.
$server1 = "hl1master.steampowered.com";
$server2 = "72.165.61.190";
$port = 27010;
$timeout = 10;
echo "Timeout = ",$timeout,"s\n";
 
$fp = fsockopen("udp://$server1", $port, $errno, $errstr, $timeout);
if (!$fp) exit("Error $errno : $errstr");
stream_set_timeout($fp, $timeout);
 
// region=world, filter: game=Team Fortress Classic
if (!fwrite($fp, "1\xFF0.0.0.0:0\0\\gamedir\\tfc\0")) {
  fclose($fp);
  exit("fwrite error\n");
}
 
$s = bin2hex(fread($fp, 6));
$info = stream_get_meta_data($fp);
if ($info['timed_out']) {
  fclose($fp);
  echo "Server 1 timed out, trying server 2.\n";
 
  $fp = fsockopen("udp://$server2", $port, $errno, $errstr, $timeout);
  if (!$fp) exit("Error $errno : $errstr");
  stream_set_timeout($fp, $timeout);
 
  // region=world, filter: game=Team Fortress Classic
  if (!fwrite($fp, "1\xFF0.0.0.0:0\0\\gamedir\\tfc\0")) {
      fclose($fp);
      exit("fwrite error\n");
  }
 
  $s = bin2hex(fread($fp, 6));
  $info = stream_get_meta_data($fp);
  if ($info['timed_out']) {
      fclose($fp);
      exit("Server 2 timed out, aborting.\n");
  }
}
if ($s !== "ffffffff660a") {
  fclose($fp);
  exit("Expected ff ff ff ff 66 0a (hex) but got $s.");
}
 
$count = 0;
do {
  $a1 = ord(fread($fp,1));
  $info = stream_get_meta_data($fp);
  if ($info['timed_out']) {
      echo "Time out occured.\n";
      break;
  }
 
  // Let's always read the rest of the address, even if it starts with 0.
  // This may avoid subsequent problems, but I'm paranoid here.
  $a2 = ord(fread($fp,1));
  $a3 = ord(fread($fp,1));
  $a4 = ord(fread($fp,1));
  $a5 = ord(fread($fp,1))*256 + ord(fread($fp,1));
 
  if ($a1 != 0) {
      $count++;
      echo "$count $a1.$a2.$a3.$a4:$a5\n";
  }
} while ($a1 != 0);
echo "Retrieved $count servers.\n";
 
fclose($fp);
?>
</pre>

Latest revision as of 08:48, 16 October 2021

This is a code example for querying a Half-Life master server using the PHP language.
See also User:Pizzahut/test.php - a test script in PHP to check which master servers respond.