473,409 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Server Detector

Hi guys,

I've written a small bit of PHP code who's purpose is to detect my game
server. If the game server is running, I want to display the Launcher and
Client population (two parts of the game program).

So here is what I wrote, minus the address and port :)

<?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);

$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]);
echo "<CENTER>The Sovereignty Server<BR>";
echo "is UP</CENTER><BR>";
?>
<CENTER><img src="GreenGem.png" border="0"></CENTER>
<?php
echo $recvbuf . $y . "<BR>";
echo "Launcher Population:" . $LauncherPopulation . "<BR>";
echo "Client Population:" . $ClientPopulation . "<BR><BR>";
}
else
{
echo "<CENTER>The Sovereignty Server<BR>";
echo "is DOWN</CENTER><BR>";
?>
<CENTER><img src="RedGem.png" border="0"></CENTER>
<?php
}
}
socket_close($sock);
?>

This works to some extent. If the server is not running, I get a timeout
after 3 seconds and the red gem graphic is shown. If it is running the
server responds and I get the green gem graphic. The server sends back two
integers for the population

The problem is the $LauncherPopulation and $ClientPopulation values. They
always come up 0 even when there are player in game. The result of the
socket_recvfrom call ($y) is 8, which is the proper size for two integers,
and I know for sure that the server is sending the correct values. But for
some reason, my buffer doesn't appear to be set with them.

What am I doing wrong?

Ron
--
Creation is an act of sheer will
www.rjcyberware.com
Home of "Manifest Destiny" and "Sovereignty"
Jul 17 '05 #1
2 2455
What do you mean exactly that you said "The server sends back two
integers?" Is the server sending two integers printed out as text ("45
64") or two integers in binary? If it's sending back two binary 32 bit
numbers, then you need to use unpack:

$a = unpack("Nlp/Ncp", $recvbuf);
$LauncherPopulation = $a['lp'];
$ClientPopulation = $a['cp'];

I'm assuming big-endian packing order here, since you said you're
getting zero from recvbuf[0] and recvbuf[4].

If the numbers are returned in a text string, then you need to extract
them using sscanf(), preg_match() or whatever.

"Ron Hiler" <rh****@spam.be.gone.rjcyberware.com> wrote in message news:<3fc91acf$1@wobble>...
Hi guys,

I've written a small bit of PHP code who's purpose is to detect my game
server. If the game server is running, I want to display the Launcher and
Client population (two parts of the game program).

So here is what I wrote, minus the address and port :)

<?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);

$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]);
echo "<CENTER>The Sovereignty Server<BR>";
echo "is UP</CENTER><BR>";
?>
<CENTER><img src="GreenGem.png" border="0"></CENTER>
<?php
echo $recvbuf . $y . "<BR>";
echo "Launcher Population:" . $LauncherPopulation . "<BR>";
echo "Client Population:" . $ClientPopulation . "<BR><BR>";
}
else
{
echo "<CENTER>The Sovereignty Server<BR>";
echo "is DOWN</CENTER><BR>";
?>
<CENTER><img src="RedGem.png" border="0"></CENTER>
<?php
}
}
socket_close($sock);
?>

This works to some extent. If the server is not running, I get a timeout
after 3 seconds and the red gem graphic is shown. If it is running the
server responds and I get the green gem graphic. The server sends back two
integers for the population

The problem is the $LauncherPopulation and $ClientPopulation values. They
always come up 0 even when there are player in game. The result of the
socket_recvfrom call ($y) is 8, which is the proper size for two integers,
and I know for sure that the server is sending the correct values. But for
some reason, my buffer doesn't appear to be set with them.

What am I doing wrong?

Ron

Jul 17 '05 #2
Thanks for the response, Chung. I was sending the ints binary, which turned
out to be the problem.

I got it, though. My web site server uses a slightly older version of php
(4.2.3, Jan 8 2003), and apparaently the socket_recvfrom() function doesn't
support binary data in that version. I changed to using plaintext instead
of binary, and the values came through fine :)

Ron
--
Creation is an act of sheer will
www.rjcyberware.com
Home of "Manifest Destiny" and "Sovereignty"
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RonHiler | last post by:
Hi guys, I run a game server (for a game I'm writing in development). I had a routine on my web site which detected if the server was up or down and displayed a icon, so that people could see...
2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
4
by: Chinmoy Mukherjee | last post by:
Hi All, Do you know of any free memory leak detector for C++ for windows OS? Regards, Chinmoy
2
by: Leon Lambert | last post by:
I was wondering if someone could post some links to some bug pattern detector software for C#. I found this very interesting article about pattern detectors for Java so wanted to investiage one for...
3
by: ME | last post by:
I would like to build a portscan detector in vb.net but I have no idea how to start. Can anybody give me any ideas on how to start, what kind of code to use? Regards Geert
5
by: Jacob | last post by:
How do I install Universal Encoding Detector (http://chardet.feedparser.org/)? Thanks, Jacob
1
by: Lighter | last post by:
Is there a way to write a memory leak detector supporting new(nothrow)? For example, #include <My_Debug_New.h> using namespace std; int main() {
0
by: Dmitriy V'jukov | last post by:
I want to announce release 1.1 of Relacy Race Detector. First of all, now you can freely DOWNLOAD latest version of Relacy Race Detector DIRECTLY FROM WEB:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.