Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Socket help, trying to make server monitor
Old 12-22-2007, 05:07 PM PHP Socket help, trying to make server monitor
Skilled Talker

Posts: 71
Trades: 0
I'm trying to make a game server monitor. This script works perfectly well on my localhost (using AppServ as my WAMP), but does not work when I upload to my hosting. I have two hosting accounts with different companies and it doesn't work for either, and I get the same error.

The error is "Buffer Empty!", which means it's not reading/writing the information.

Any help would be awesome, thanks.

Here is my source:

Code:
<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>';

?>
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-23-2007, 02:12 PM Re: PHP Socket help, trying to make server monitor
Skilled Talker

Posts: 71
Trades: 0
Anyone??
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Old 12-24-2007, 02:14 AM Re: PHP Socket help, trying to make server monitor
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
remove the @ from fsockopen first
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-24-2007, 02:16 AM Re: PHP Socket help, trying to make server monitor
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Also print out what is contained in your error variables, that is what they are for and it is called debugging.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-24-2007, 05:09 PM Re: PHP Socket help, trying to make server monitor
Skilled Talker

Posts: 71
Trades: 0
I removed the @ and printed errno and errstr. errno returned "0", and errstr returned nothing.

It is connecting fine. The problem comes when I try to read/write the information.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Old 12-24-2007, 07:25 PM Re: PHP Socket help, trying to make server monitor
Novice Talker

Posts: 13
Name: hach22
Trades: 0
PHP Code:
stream_set_blocking ($fp,1);//second parameter is an integer
stream_set_timeout  ($fp,10);//1 is a small timeout!!! 
PHP Code:
<?php

/**
 * @Author 
 * @Copyright 2007
 */



$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 '
<html>
<head>
    <title>COD4 SERVER MONITOR</title>
</head>
<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>
<body bgcolor=#000000>'
;

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

$fp fsockopen ('udp://'.$ip$port$errno$errstr1);

stream_set_timeout  ($fp100);
stream_set_blocking ($fptrue);
if (!
$fp) {
    print 
'Connection Refused!';
}


fwrite ($fp$challenge);

$buffer fread ($fp4096);

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) - 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></body></html>';

Last edited by hach22; 12-24-2007 at 07:39 PM.. Reason: Commented and edited
hach22 is offline
Reply With Quote
View Public Profile
 
Old 12-25-2007, 09:21 PM Re: PHP Socket help, trying to make server monitor
Skilled Talker

Posts: 71
Trades: 0
Thanks, but that does the same thing. It works on my localhost but not on my webhost.

www.forsakenwarfare.net/monitor3.php

This is what I get.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Socket help, trying to make server monitor
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50695 seconds with 12 queries