#### Archived

This topic is now archived and is closed to further replies.

# Detecting my Server on my Website

This topic is 5131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I''m coding away on my mutliplayer game, and I''ve gotten it to the point where players can log on and walk around my virtual world and see other players and blah blah blah What I would now like to do is show some sort of status on my web page about the server. That is to say, when the server is running, I''d like for the page to show that fact (with a little green light or something), and what would be REALLY nice is if I could show the server population as well. Something like "The server is UP, 12 people are logged in", or "The server is DOWN". Any tips on how to do this? I know enough PHP to get by, though it''s not my native tounge Should I somehow send some sort of packet to my server and look for a response every few minutes? I''m not entirely sure how to send a packet to a specific IP like I can in C++, heh. In my code I use WinSock. Tips are appreciated! -Ron

##### Share on other sites
While you could get complex and have PHP directly connected into your game, the easiest and perhaps best in terms of stability would be to simply have a file like "serverstatus.php" which is updated every 60 seconds by your game server.

Something like:

<?
$LastUpdate = A timestamp of when this file was last written by the game server$Population = blah
//Other game stats

function IsServerUp()
{
return ( time()-120 < $LastUpdate ); } ?> Then in your website scripts you can just include_once("serverstatus.php"); #### Share this post ##### Link to post ##### Share on other sites That''s interesting! Yes, I like the idea. However, I think there is a snag. My game server sits in my office, and my web server is somewhere on the east coast How would I have my game server write to a file that is on my web server? #### Share this post ##### Link to post ##### Share on other sites quote: Original post by RonHiler That's interesting! Yes, I like the idea. However, I think there is a snag. My game server sits in my office, and my web server is somewhere on the east coast How would I have my game server write to a file that is on my web server? If they are on machines connected only by an unprotected wan (like the Internet) then I would suggest not trying to write a file remotely (you could do it a secure manner, but it would require that you write a lot of stuff into your game, like a complete HTTPS client from scratch). Instead I would suggest that you have a php script on your webserver that either writes the file, or writes the details to a database like MySQL (the database approach would allow you to do interesting things like have uptime reports and such) using parameters provided by a form (you don't need to have the form anywhere, only know what the value names are). A very simple way to make it secure would be this: Make a mini HTTP client on your game server (it doesn't need to understand much, just how to send a GET command which includes form parameters, not very hard over TCP) and have it send the various details (server status and such) as form data. For (easy to impliment, fairly strong) security two more details should be used. The first is the game server time (this is compared to the reported game server time of the last update. If it is equal or less the information sent is rejected). The second piece of information is the MD5 hash of all the previous information (server stats and server time) plus an additional, secret value (a key of whatever length you choose). When recieved by the php script it attempts to recreate this hash value itself (by combining the server stats and key and using the MD5() function on the resulting string). If the hash provided by the client and the hash generated by the php script don't match the information is rejected as false. For added security you could also have it check the IP address of the client and compare it to the known IP address of the game server. [edited by - michalson on November 26, 2003 7:26:47 PM] #### Share this post ##### Link to post ##### Share on other sites Why not use the socket functions built into PHP to connect to the game server every time a user requests the page, and if the server responds then that means the server is up? http://jamesthornton.com/php/english/ref.sockets.html (the php site is down right now) [edited by - Firewalker on November 26, 2003 7:37:57 PM] #### Share this post ##### Link to post ##### Share on other sites As an alternative you could switch over to using ASP and program a Web Service that you could then access the servers through a standard winsock interface. This could allow you to choose your poison...C++, C#, VB. #### Share this post ##### Link to post ##### Share on other sites quote: Original post by Synth0id As an alternative you could switch over to using ASP and program a Web Service that you could then access the servers through a standard winsock interface. This could allow you to choose your poison...C++, C#, VB. have you ever tried php?? if not you will most likely ditch asp instanly once you try besides that php runs on nearly every system whereas asp... you know. #### Share this post ##### Link to post ##### Share on other sites Don't bother LousyPhreak... I know full and well the pros and cons of each language. .NET Webservices are an awesome feature of ASP, so grow up and stop turning this into a langauge war. synth0id [edited by - synth0id on November 27, 2003 5:53:48 PM] #### Share this post ##### Link to post ##### Share on other sites Synth0id you take everything that dead serious do you? btw to make this post a bit more useful than the last 2 here''s some code to query a halflife server in php, just to get you (RonHiler) an idea.$command can be a string like ''info'', for more you need to look at the specs in the hl-sdk.

function sendCommand($command,$server=''127.0.0.1'',$port=27015) { if(($sock = socket_create (AF_INET, SOCK_DGRAM, 0)) < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($sock) . "\n"; }$send=sprintf(''%c%c%c%c%s%c%c'',255,255,255,255,$command,0,0);$x=socket_sendto($sock,$send,strlen($send)-1,0,$server,$port);$y=socket_recvfrom($sock,$recvbuf,1000,0,$from,$port);
return $y; } #### Share this post ##### Link to post ##### Share on other sites Ah, that''s very interesting LousyPhreak, it looks like it would work okay with my setup. I''ll give it a try #### Share this post ##### Link to post ##### Share on other sites This is gamedev.net...I have to always be on my guard I''m so used to people slamming everything that I say that it has become nearly automatic to instantly defend my knowledge. Gotta love this place... Anyways, sounded to me like you were gung-ho on starting another infamous language-war. Nothing in your post had to do with the thread itself and so I assumed the obvious. Maybe it''s just me though I doubt it synth0id #### Share this post ##### Link to post ##### Share on other sites RonHiler: if you are interested i could give you some more details as this is just a quite old piece of code not really showing what the games does. #### Share this post ##### Link to post ##### Share on other sites No need, I don''t think LP. I used a variation on your code snippet from my website, and my server picked it up! Just like it would a regular packet from one of the clients. Very cool! It doesn''t yet know what to do with the packet, so it''s complaining about an unknown packet type, but that''s just because I haven''t coded it into the server code yet. I get the same thing whenever I code a new packet into the clients. I''ll try responding with the population data and see if the web site server gets it back. I don''t see why it wouldn''t, though. It seems like this might work fine! Yay If I have any problems I''ll probably ask for further help, but I think I''ve got it from here. Thanks a lot for the advice guys, this will be a very cool addition to my site. -Ron #### Share this post ##### Link to post ##### Share on other sites I got so *very* close! Unfortunately, I ran into a snag. LP, or anyone, perhaps you can tell me what I'm doing wrong. Unfortunately, these socket functions are "experimental" and therefore documentation seems to be pretty non-existant. Anyway, the website sends a packet and the server, if it's up and running, sends a response of two ints (basically the Launcher population and the Client population). This gets back to the web server, where it seems to get there okay (the result of socket_recvfrom is 8, the number of bytes I sent from the server). But my recieve buffer seems to be empty! Here is the php code (I'm taking out my server address and port, in the actual code they are there): <?php if(($sock = socket_create (AF_INET, SOCK_DGRAM, 0)) < 0)
{
echo "socket_create() failed: reason: " . socket_strerror($sock) . "<BR>"; } else {$server='xxx.xxx.xxx.xxx';
$port=yyyy;$send=sprintf('%c%c%c%c',24, 0, 0, 0);
$x=socket_sendto($sock,$send,strlen($send)-1,0,$server,$port);

//don't allow blocking, do a 3 second timeout

//if we don't get a response by 3 seconds, the server probably ain't there...

$bar = array($sock);
$select_result = socket_select($bar, $b=null,$c=null, 3);
if ($select_result == true) {$y=socket_recvfrom($sock,&$recvbuf,1024,0,&$from,&$port);

$LauncherPopulation = sprintf('%d',$recvbuf[0]);
$ClientPopulation = sprintf('%d',$recvbuf[4]);
//check the results

echo $recvbuf .$y . "<BR>"; //($y = 8,$recvbuf has no output??)

echo "Launcher Population:" . $LauncherPopulation . "<BR>"; echo "Client Population:" .$ClientPopulation . "<BR><BR>";
//both population variables report 0, even when players in game????

}
else
{
echo "Server Timeout<BR><BR>";
}
}
socket_close(\$sock);
?>

As I said, this works fine for detecting if the server is running or not, (the if statement goes through the true or false path depending on the server status). I just can't seem to pull those 8 bytes I've received out into variables. Any suggestions?

[edited by - RonHiler on November 29, 2003 4:20:26 PM]

[edited by - RonHiler on November 29, 2003 5:24:16 PM]

##### Share on other sites
I got it. It turns out that socket_recvfrom() can''t deal with binary data, only alpha-numberic text. So I turned the population data into strings at the server end and passed them that way, and everything works great now.

##### Share on other sites
now as you mention it i remember the exact same problem back when i was coding it (yea its *really* old )

they should've fixed it by now....

edit:
just checked - this bug is gone (php 4.3.3 linux)

actually its almost a year dead by now. what php version are you using?

[edited by - LousyPhreak on December 1, 2003 12:57:08 PM]