Jump to content

http and game server status check? (udp problems..)


Bkid

Recommended Posts

Disclaimer: I've been messing around with PHP for a little over a day, so the fact that I've gotten as far as I have is nothing short of a miracle. :P

 

I've tried my best to figure things out on my own, but it's come down to this.. There's a game I play online, and the game server's hostname is the same as the website itself. That's not really the problem, however. I've made a page to check the status of the site + the game server (using different ports), but the game server isn't wanting to play nice (big surprise).

 

I think I've done the website part correctly, although there may been a better way to go about doing it (ping instead?). It seems to work, but then again, the site hasn't been down yet. That being said, I can change it to a made up site and it will say "Offline", so I guess it's ok.

 

The game server part is what gets me. Basically, because of the way the server is, I have to make a UDP connection, send data, and listen for a reply. Based on whether I get a reply or not is how I can tell if it is online or not. The code is currently either not working, or returning false-positives (when the server is actually down, or I change the port to something I know is not open).

 

THE CODE! (I'll just post the whole thing, html and all):

 

<html>
<head>
<title>Server Status</title>
<meta http-equiv="refresh" content="30">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body bgcolor="#212F3A">
<center> 
<font color="white"><h1>Server Status:</h1></font>
<br />
<?php
$host = "example.com";
$port = "80"; 
$port2 = "7500";
echo '<FONT COLOR=white>Website Status:</FONT> ';
if (!$sitestatus = @fsockopen($host, $port, $num, $error, 10)) 
echo '<B><FONT COLOR=red>Offline</b></FONT>'; 
else{ 
echo '<B><FONT COLOR=lime>Online</b></FONT>'; 
fclose($sitestatus);
}
echo '<br />';
echo '<FONT COLOR=white>MTSP Status:</FONT> ';
$fp = @fsockopen("udp://$host", $port2, $num, $error, 10);
if($fp = @fsockopen("udp://$host", $port2, $num, $error, 10) && fwrite($fp, "0")) {
echo '<B><FONT COLOR=lime>Online</b></FONT>';
}
else {
echo '<B><FONT COLOR=red>Offline</b></FONT>';
}
/* Another way I was trying to do it...
$fp = @fsockopen("udp://$host", $port2, $num, $error, 10);
if(!$fp = @fsockopen("udp://$host", $port2, $num, $error, 10) && !fwrite($fp, "0")) {
     echo 'Not Connected';
}
else {
     echo 'Connected';
 $reply = fread($fp, 30);
 echo '<br>Server reply was: $reply';
 fclose($fp);
} */
echo '<br /><br />';
echo '<FONT COLOR=white>(This page will automatically refresh every 30 seconds.)</FONT>';
?>
</body>
</html>

 

 

Here is a portion of the code from the server it's trying to connect to, which I think I can use to check it's status:

 

		switch(rcv[0]){
	case PH_PING:
		rcv[0] = PH_PONG;
		UDP->Send(rcv, rcv->Length, ep);
		break;

	case PH_PONG:
		if(rcv->Length == 1 && Ping > 0){
			form->WriteMessage(String::Format("Ping : {0}ms\n", timeGetTime() - Ping), SystemMessageColor);
			Ping = 0;
		}
		break;

 

My questions:

  • How do I send data to the server, and how do I listen for a reply and act upon it?
  • Is meta-refresh necessary to make sure the page isn't showing the server offline when it's actually not (or vice versa)?
  • Is cache-control necessary, or is the script going to re-run each time, producing the correct results (I think it's not necessary, but I'm not sure..)?

 

 

Phew, that was a lot..Anyway, I think that's about it, and I'll edit my post if it isn't. :P

Thanks for any help! :D

I've modified your script a bit, to show you a more flexible way of doing things. Also, added some error checking, and comments.

<?php

$host = "example.com";
$port = "80";
$port2 = "7500";

/**
* Check if a game's website and server is running, and returns an array with the results.
* 
* @param string $host
* @param int $webPort
* @param int $gamePort
* @return array
*/
function check_game ($host, $webPort, $gamePort) {
// Open a cURL request to the web server, and retrieve header data.
$cReq = curl_init ('http://'.$host);
curl_setopt ($cReq, CURLOPT_HEADER, true);

// Check if the server returned 200 (OK).
if (curl_getinfo ($cReq, CURLINFO_HTTP_CODE) == 200) {
	$siteStatus = 'online';
} else {
	$siteStatus = 'offline';
}

// Open a connection to the game server, and request its status.
$fp = fsockopen ("udp://$host", $gamePort, $num, $error, 10);
if (!$error) {
	// TODO: Add status request payload.
	fwrite ($fp, "0");
}

// Read the reply from the server, if we get something we're good.
if (!$error && fread ($fp, 1024)) {
	$gameStatus = 'online';
} else {
	$gameStatus = 'offline';
}

// Return the status messages.
return array ($siteStatus, $gameStatus);
}

$status = check_game ($host, $port1, $port2);
$status = <<<OutHTML
<dl>
<dt>Website status:</dt>
<dd>{$status[0]}</dd>
<dl>MTSP status:</dl>
<dd>{$status[1]}</dd>
</dl>
OutHTML;

?>

<html>
<head>

<title>Server Status</title>

<meta http-equiv="refresh" content="30">
<meta http-equiv="cache-control" content="no-cache">

</head>
<body>

<h1>Server Status:</h1>
<?php echo $status; ?>

<p class="footer">(This page will automatically refresh every 30 seconds.)</p>

</body>
</html>

 

For styling the HTML elements, you'll want to use CSS. It's a much better way, and by writing standard compliant HTML you will save yourself from a lot of headaches. (Note that the HTML above is not quite standards compliant yet, you're missing a doctype and possibly a content header.)

 

If I've used something you don't understand, I recommend looking at the PHP manual for more information. Should you still have some question after reading the manual, and playing around with the code for a bit, please let me/us know.

 

The meta-refresh isn't the only way to solve it, but it's the simplest way to accomplish what you want. Another way of doing it is AJAX (use jQuery), but that is a lot more technically advanced. So I'd work on the basics a bit more, before venturing there, if I were in your shoes. ;)

 

Cache-control shouldn't be necessary, but it never hurts to be on the safe side.

 

In closing: Excellent first post. :)

Thanks a bunch! I'm not on my computer right now, but I'll edit my index file and uploaded it to see how it works out. I haven't used html in forever, and I'm extremely new to php, but I'm getting there. :P I didn't really care about the html and standard compliance too much, since it was just a simple little server status page, but I might clean it up later. I'm trying to teach myself a little bit right now, and after I graduate for networking I'll probably go back for programming. ;)

 

Again, thanks so much for helping me out with this. I'll be testing it soon.

I just tried the code, and it wasn't really working out as planned at first. The website was coming back offline, even though it wasn't. I added the following to test it:

 

$httpstatus = curl_getinfo ($cReq, CURLINFO_HTTP_CODE);
echo 'Server status code: ' . $httpstatus;

 

I found that it was returning 0, and that was not good at all.

 

I did some research, and rewrote some of it as the following:

 

<?php
$host = "example.com";
$port = "80";
$port2 = "7500";
function check_game ($host, $webPort, $gamePort) {
// WEBTSITE STATUS
$cReq = curl_init ('http://'.$host);
curl_setopt ($cReq, CURLOPT_HEADER, true);
curl_setopt($cReq, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cReq, CURLOPT_CONNECTTIMEOUT, 5);
curl_exec($cReq);
$httpcode = curl_getinfo ($cReq, CURLINFO_HTTP_CODE);
if (curl_getinfo ($cReq, CURLINFO_HTTP_CODE) == 200) {
	//The end part on each one below was only there for testing.
	$siteStatus = "<B><FONT COLOR=lime>Online </b></FONT>(" . $httpcode . " was returned correctly.)";
} else {
	$siteStatus = "<B><FONT COLOR=red>Offline (</b></FONT>(" . $httpcode . " was returned instead of 200.)";
}
curl_close($cReq);

// GAME SERVER STATUS
// Open a connection to the game server, and request its status.
$fp = fsockopen ("udp://$host", $gamePort, $num, $error, 5);
if (!$error) {
	// TODO: Add status request payload.
	fwrite ($fp, "0");
}
// Read the reply from the server, if we get something we're good.
if (!$error && fread ($fp, 1024)) {
	$gameStatus = '<B><FONT COLOR=lime>Online</b></FONT>';
} else {
	$gameStatus = '<B><FONT COLOR=red>Offline</b></FONT>';
}
fclose($fp);
// Return the status messages.
return array ($siteStatus, $gameStatus);
}

$status = check_game ($host, $port1, $port2);
$status = <<<OutHTML
Website status: {$status[0]}
<br />
MTSP status: {$status[1]}
OutHTML;

?>

<html>
<head>

<title>Server Status</title>

<meta http-equiv="refresh" content="30">
<meta http-equiv="cache-control" content="no-cache">

</head>
<body bgcolor="#212F3A">
<center> 
<font color="white"><h1>Server Status:</h1>
<br />
<?php echo $status;?>
<p class="footer">(This page will automatically refresh every 30 seconds.)</p></font></center>

</body>
</html>

 

 

The game server part still doesn't seem to work correctly, although I'm not sure why. :(

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.