Jump to content

[SOLVED] PHP socket help, trying to make server monitor


CrazeD

Recommended Posts

Hi I'm trying to make a server monitor for Call of Duty 4. Everything works fine on my localhost, but when I upload it to my server, it doesn't work.

 

<body bgcolor='#000000'>

<?php

$ip = '8.9.9.13'; // Server IP
$port = '28960';  // Server Port

$cellColor   = '#101010'; // Cell Background Color
$borderColor = '#9d9d9d'; // Table Border Color
$fontColor   = '#ffffff'; // Text Color

$width = '300px'; // Overall Table Width

print '<style type="text/css">
.gentext {
font-family: serif;
color: '.$fontColor.';
font-size: 12pt;	
}

.infotable {
border: 1px;
border-style: solid;
border-width: thin;
border-color: '.$borderColor.';
border-collapse: collapse;
}
</style>';

$challenge = "\xFF\xFF\xFF\xFFgetstatus";

$fp = @fsockopen ('udp://'.$ip, $port, $errno, $errstr, 1);

if (!$fp) {
print 'Connection Refused!';
}

stream_set_timeout  ($fp, 1, 0);
stream_set_blocking ($fp, true);

fwrite ($fp, $challenge);

$buffer = fread ($fp, 4096);

fclose ($fp);

if (!$buffer) {
print 'Buffer Empty!';
}

$rawdata = explode ("\n", $buffer);

array_pop ($rawdata);

$rawitem = explode ("\\", $rawdata[1]);

foreach ($rawitem as $key => $value) {
$value             = strtolower ($value);
$value             = preg_replace ("/\^./", "", $value);
$rawitem[$key + 1] = preg_replace ("/\^./", "", $rawitem[$key + 1]);

$setting[$value]   = $rawitem[$key + 1];
}

$data['gamemod']    = $setting['gamename'];
$data['hostname']   = $setting['sv_hostname'];
$data['mapname']    = $setting['mapname'];
$data['players']    = $rawdata[2] ? count($rawdata) - 2 : 0;
$data['maxplayers'] = $setting['sv_maxclients'];
$data['password']   = $setting['g_needpass'];
$data['gametype']   = $setting['g_gametype'];
$data['version']    = $setting['shortversion'];

array_shift ($rawdata);
array_shift ($rawdata);

foreach ($rawdata as $key => $value) {
preg_match("/(.*) (.*) \"(.*)\"/", $value, $match);

$player[$key + 1]['score'] = $match[1];
$player[$key + 1]['ping']  = $match[2];
$player[$key + 1]['name']  = $match[3];
$player[$key + 1]['name'] = preg_replace ("/\^./", "", $player[$key + 1]['name']);

}

print '<table border="1" class="infotable" width="'.$width.'">
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Name:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['hostname'].'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>IP:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$ip.':'.$port.'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Game:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['gamemod'].'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Map:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['mapname'].'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Gametype:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.ucfirst ($data['gametype']).'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Version:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['version'].'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Players:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['players'].'</td>
  </tr>
  <tr>
    <td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3"><b>PLAYERLIST</b></td>	
  </tr>';
  $playerCount = count ($player);
  if ($player[1]['name'] == '') {
  print '<tr>
	<td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3">The server is empty.</td>	
  </tr>';
  } else {
  print '<tr>
	<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Name</b></td>
	<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Score</b></td>
	<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Ping</b></td>
  </tr>';

  for ($i = 1; $i <= $playerCount; $i++) {
	  print '<tr>
		<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['name'].'</td>
		<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['score'].'</td>
		<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['ping'].'</td>
	  </tr>';
  }
  }

  print '</table>';

?>

 

The problem I get when I upload it is, "Buffer Empty!" It seems to have a problem with line 41, "fwrite ($fp, $challenge);"

$challenge is "$challenge = "\xFF\xFF\xFF\xFFgetstatus";"

 

I'm totally stumped here. Note that it connects fine, just that one bit on line 41 doesn't work. Is this because of my web host? I've tried 2 different hosts.

 

Please help.

Link to comment
Share on other sites

There's no such thing as "connection refused" with udp, as opening a udp "connection" actually does not communicate with the server.  Given that, it's possible your packet is being blocked (or the return packet is being blocked) even though the connection succeeds.

 

One thing you may want to try is waiting before doing the fread().  Perhaps there's some problem with the fread() not blocking like you expect it to. 

Link to comment
Share on other sites

You could try adding sleep(1); before calling fread().  That will wait for 1 second before checking for data, which might make a difference.  I would also try a 10 second delay as well.  If neither of those work, it's back to the drawing board.

Link to comment
Share on other sites

sleep (10) didn't work.

 

I put this in:

if (!fwrite ($fp, $challenge)) {
print 'fwrite() failed<br />';
}

 

And it didn't display the error, so I guess the problem would be with fread then, eh?

 

Do you think it's a problem with my code or with the hosting (as in, not allowed)?

 

Thanks for the help.

Link to comment
Share on other sites

It may well be the hosting.  Even if you successfully fwrite() and the udp packet is sent, the host may still block it and you will never know.  You'll need to ask your host if they allow that (and hope that they know enough about networking to actually give you an answer!)

 

You can just say "I am trying to send a udp packet to port x on ip x to test if a server is online.  Is this allowed by x hosting company?"

 

Given that your code works on your own pc, I would assume it's an issue with the hosting.

Link to comment
Share on other sites

just a side note, why don't you open yoru conenction on 80, bind a listener, and then send from that socket on port 80 ?  That would then be artificially bypassing their port blocks by sending on their port 80.

 

Because I don't know how to do that. :P

 

It's okay though, I have it running on my dedicated game server box now.

 

http://72.37.226.250/monitor.php?ip=72.37.226.250:28960

 

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.