CrazeD Posted December 22, 2007 Share Posted December 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/ Share on other sites More sharing options...
phpSensei Posted December 22, 2007 Share Posted December 22, 2007 Post line 41 please. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-421046 Share on other sites More sharing options...
CrazeD Posted December 22, 2007 Author Share Posted December 22, 2007 I did... Line 41 is: fwrite ($fp, $challenge); I'm not positive but I think this is where it's going bad. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-421081 Share on other sites More sharing options...
phpSensei Posted December 22, 2007 Share Posted December 22, 2007 Not sure, but check the ip, or $fp = @fsockopen ('udp://$ip', $port, $errno, $errstr, 1); Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-421085 Share on other sites More sharing options...
CrazeD Posted December 22, 2007 Author Share Posted December 22, 2007 The IP is correct. What exactly am I checking on that line? It works fine on my localhost, remember. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-421087 Share on other sites More sharing options...
CrazeD Posted December 26, 2007 Author Share Posted December 26, 2007 Anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-423774 Share on other sites More sharing options...
btherl Posted December 27, 2007 Share Posted December 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-423840 Share on other sites More sharing options...
CrazeD Posted December 27, 2007 Author Share Posted December 27, 2007 Thanks for the reply. Can you elaborate "wait till before fread"? Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-423872 Share on other sites More sharing options...
btherl Posted December 27, 2007 Share Posted December 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-423937 Share on other sites More sharing options...
CrazeD Posted December 27, 2007 Author Share Posted December 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-424023 Share on other sites More sharing options...
btherl Posted December 28, 2007 Share Posted December 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-424389 Share on other sites More sharing options...
CrazeD Posted December 28, 2007 Author Share Posted December 28, 2007 Yup....I emailed the support and they said every port other than 80 and 443 are blocked. Bunk. Oh well, I'll just use my dedicated server box instead...hehe. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-424855 Share on other sites More sharing options...
d22552000 Posted December 28, 2007 Share Posted December 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-424920 Share on other sites More sharing options...
CrazeD Posted December 29, 2007 Author Share Posted December 29, 2007 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. 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 Quote Link to comment https://forums.phpfreaks.com/topic/82789-solved-php-socket-help-trying-to-make-server-monitor/#findComment-424983 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.