Check out Half-Life Re-imagined competition results!
Check out Skewing textures in Hammer, our newest tutorial!
Say hello to timadam, our newest member!
Introduction
This article is a reference to a method of using the server protocol with PHP. If you are not familiar with PHP, see the tutorials on the PHP website. The method is compatible with Steam dedicated servers. With the Half-Life server protocol, it is simple to send requests to the WON master servers, and standard game servers via almost any programming language. For this tutorial, I will be using PHP. We will send a request to a regular Half-Life game server and parse the data that can be used on a website for live server information. Half-Life Server Protocol With the Half-Life server protocol, all packets sent to the server must be sent with four consecutive bytes of 255, then a specific string command and end it with a byte with a zero value. To send a 255 byte value with PHP you will use the octal value in your string. With the server protocol, there are several string commands you can send. For now, I will only explain one command. If you wish to use other commands, check out the Server Protocol.txt in the SDK. The infostring command is not documented in the document provided in the SDK. I found this command by looking at the raw packets with a packet sniffer when the Half-Life server browser refreshed its list. The packets returned after you send the infostring command are nicely formatted with back slashes between values so they can easily be placed in an array using some common PHP functions. Sending the Request To send the request, we must first open a connection to the server. The Half-Life game sever uses a UDP port for client connections. You use the same port to the server as you would if you were connecting through Half-Life. For the example, the server is running on 127.0.0.1 (localhost) with 27016 as the port. To call a connection with PHP, you use the fsockopen function:
With the above code, it sends the command to the server with the fwrite function. Then a while loop creates a variable with all the data returned from the server. When this code is run, you should receive the results of the infostring command. Parsing the Data Now, we have the raw data returned from the server. We should be able to parse the data to be used on a web page. Now, we will split the string into an array, then assign each returned value into another organized array for easy use.
<?php
Returned Values Returned data from the code will return some commonly used information about the server. Here is a list of keys you can use use and a description of what they are:
address - IP Address of the server
Now, if you're going to use the data in a webpage, the data is all stored in the $ServerData array. You can now use the following code snippet, simply changing "VALUENAME" to the key of the value you wish to display.
simply use This method is useful for Gaming websites; for example, displaying the status of a Clan's server. This is for you to do, so please do not email the author requesting custom scripts to be written. |