Going about programming a minecraft server status checking program - php

I'm wanting to make a program which allows for me to check the status of Minecraft servers whether they're online, offline or full. How would I go about doing so? I'm thinking PHP server side, Python client side and SQL as server?
I need some major advice on what needs to be done to achieve such a task.

You don't need a database. Ping the server. If it responds, it's up...display a large green checkmark. If it doesn't respond...it's down. Display a large red x. You can do that in the FB API or in plain PHP anywhere.
See this question, which provides the following code:
function ping($host, $port, $timeout)
{
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "down"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}

Notably if you want more information than just the server up/down status you can use the MineQuery protocol that most servers have enabled.
More info # DinnerBone's tool.
This will allow you to get the current/max players, MOTD, game version, and some other details if the server uses CraftBukkit.
Also this version written in PHP.

Related

How to check whether server IP status is up or down in PHP

I am creating a web application where I will be adding 10-20 IP addresses and setting a cron job for every 1 hour, that can be done easily. The problem is that I want to check whether an IP is currently working or not. If the IP is not working then I will get a message by email or some other mode. I have found the below code on the Internet:
$ip= '103.117.231.160';
$port = 80;
$fp = #fsockopen($ip, $port, $errno, $errstr, 2);
if (!$fp) {
echo 'offline';
} else{
echo 'online';
}
I have added many random IPs, but it's giving me online for the status. Is the above script fine or do I need to change the script? How can I test that an IP is working or not? Can anyone provide sample test IPs so I can make sure the script is working fine?
I just need correct output via email that my server is down.

PHP Sockets in Realtime

Background
A desktop application on a user's computer gets the phone number from the modem and sends it to a PHP script once a phone call is recieved. At the moment, I am able to receive the data/packet on the specified port via PHP. I then have a scraper that connects to 411 databases and returns the address for the specified phone number.
Problem
After I retrieve the phone number in PHP through sockets, how can I automatically update the 411 parser page with the new phone number?
Code
socket_listener.php
set_time_limit(0);
$address = "127.0.0.1";
$port = 10629;
// create socket and bind with listener event
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $address, $port);
socket_listen($socket);
do {
$accept = socket_accept($socket);
$read = socket_read($accept, 1024) or die("Could not read input\n");
// parse phone number
$phone = substr($read, 19, 15);
$file = fopen("phone_numbers.txt", "a");
fwrite($file, $phone . "\r\n");
fclose($file);
} while (true);
phone_numbers.txt
(425) 555-1212
(123) 456-7890
Current Solution
My current solution is pretty quirky.
modem -> desktop application -> socket_listener.php -> data.txt -> 411_scraper.php
socket_listener.php listens on the port for incoming packets 24/7 and appends new phone numbers it receives to a text file
411_scraper.php checks text file for updates every 5 seconds. If file version is changed, then it reads the last phone number
Run code to query 411.com and retrieve data using phone number
Desired Solution
socket_listener.php listens on port for incoming packet containing phone number
Page automatically updates with new data retrieved from 411
Things I've Been Looking At
I've looked at node.js, Ratchet (www.socketme.com), and Pusher (http://pusher.com/) but they are all above my level of understanding. Pusher and Ratchet seems promising but I have not jumped into them yet.
I would use ajax or something along those lines. PHP isn't a real time thing. When you navigate to a website which uses PHP the code is executed before the page is rendered. It's processing the code on the server computer and giving you back the result as a string which you can send to the requesting client.
You're going to have the hold the data somewhere persistently. Once the php script is done executing the data is for that execution gone. Unless you use sessions.

How to identify whether server's port is accessible?

I am trying to implement a PHP script that will ping an IP on a specific port and echo out whether the server is online / offline. This way, users will be able to see if non-access to the server is a server fault or a own network problem.
The site is currently on http://Dev.stevehamber.com. You can see the "Online" is wrapped in a class of 'PHP' and I need this to reflect if the server is online or offline. The application runs on port TCP=25565 so I need the output to show if this port is reachable or not.
Here is a snippet I found that is (I suppose) what I'm looking for:
<?php
$host = 'www.example.com';
$up = ping($host);
// if site is up, send them to the site.
if( $up ) {
header('Location: http://'.$host);
}
// otherwise, take them to another one of our sites and show them a descriptive message
else {
header('Location: http://www.anothersite.com/some_message');
}
?>
How can I replicate something like this on my page?
Based on the comments on the question, fsockopen() is the simplest and most widely available way to accomplish this task.
<?php
// Host name or IP to check
$host = 'www.example.com';
// Number of seconds to wait for a response from remote host
$timeout = 2;
// TCP port to connect to
$port = 25565;
// Try and connect
if ($sock = fsockopen($host, $port, $errNo, $errStr, $timeout)) {
// Connected successfully
$up = TRUE;
fclose($sock); // Drop connection immediately for tidiness
} else {
// Connection failed
$up = FALSE;
}
// Display something
if ($up) {
echo "The server at $host:$port is up and running :-D";
} else {
echo "I couldn't connect to the server at $host:$port within $timeout seconds :-(<br>\nThe error I got was $errNo: $errStr";
}
Note that all this does is test whether the server is accepting connections on TCP:25565. It does not do anything to verify that the application listening on this port is actually the application you are looking for, or that it is functioning correctly.

PHP - echo do other client

I`m making an little php server that has 2 clients, on connects to the php (device a) and controls the other (device b).
device a makes a get request to the php. now i want to push that command to device b. is there any way to echo to device b? or to make an request to the device b?
(i only need to send one character to device b)
Pushing to device is possible but depends on your device.
A solution would be websockets, see the following links for further reading:
http://www.websocket.org/
http://code.google.com/p/phpwebsocket/
Another solution would be longpolling which is easy to implement in php:
http://en.wikipedia.org/wiki/Push_technology#Long_polling
Very simple implementation of longpolling on server-side:
$ts = time();
while(true) {
// if there's something new, send the response to the server
// if not, continue with the loop
if($response = getSuperAwesomeResponse()) {
print $response;
break;
}
// timeout after 60 seconds
if( ($ts + 60) > time()) {
break;
}
sleep(1);
}
On the client-side you just need to send some sort of ajax calls
No, unless device B is running a server of some kind (any software that accepts incoming connections really). If that's the case then you can easily make HTTP requests to the device (e.g. even with file_get_contents) or have your own custom connection protocol (with sockets). There are also other options that allow you the same functionality but work in slightly different ways.
If the device is not running any servers then the best you can do is poll the server continuously to see if there are any commands for it. This is easier to set up (the server is already there) but also not efficient.
Device B could open a client connection to the server and wait for incomming data. If data comes in, the client running on device B could echo it.
PHP offers access to network sockets, see http://www.php.net/manual/en/book.sockets.php
Some PHP example code making use of LibEvent and ZMQ, which allows a higher level of access to sockets and queues:
Event-driven Server:
<?php
// create base and event
$base = event_base_new();
$event = event_new();
// Allocate a new context
$context = new ZMQContext();
// Create sockets
$rep = $context->getSocket(ZMQ::SOCKET_REP);
// Connect the socket
$rep->bind("tcp://127.0.0.1:5555");
// Get the stream descriptor
$fd = $rep->getsockopt(ZMQ::SOCKOPT_FD);
// Define event callback function
$fnc = function ($fd, $events, $arg) {
static $msgs = 1;
echo "CALLBACK FIRED" . PHP_EOL;
if($arg[0]->getsockopt (ZMQ::SOCKOPT_EVENTS) & ZMQ::POLL_IN) {
echo "Got incoming data" . PHP_EOL;
var_dump ($arg[0]->recv());
$arg[0]->send("Got msg $msgs");
if($msgs++ >= 10) event_base_loopexit($arg[1]);
}
};
// set event flags
event_set($event, $fd, EV_READ | EV_PERSIST, $fnc, array($rep, $base));
// set event base
event_base_set($event, $base);
// enable event
event_add($event);
// start event loop
event_base_loop($base);
ZeroMQ Client:
<?php
// Create new queue object
$queue = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, "MySock1");
$queue->connect("tcp://127.0.0.1:5555");
// Assign socket 1 to the queue, send and receive
var_dump($queue->send("hello there!")->recv());
Source, Talk, Video (~22:00).

Pinging CS Servers

This has been bothering me for awhile, can some one show me how to ping a counter strike server.
I just want to ping the server and see if it is online, thats all.
I found many small snippets online that were using fsock and UDP to do this but none of them actually did the job i wanted it to do.
Most of the ones i found were showing offline servers as online.
I would really really appreciate if some one could provide me with this useful information (code).
Thank you in advance ^_^
you can't just ping the server since it uses UDP instead of normal TCP, so in order to check it's status you need to "query" the server, you can find information about what command you can send to CS servers here: http://developer.valvesoftware.com/wiki/Server_queries#A2A_PING
Anyway the following code (PHP) can be used to check CS server status.
`
`
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($socket, "cs.somesv.com", 27015);
socket_write($socket, "\xFF\xFF\xFF\xFF\x69");
if (#socket_read($socket, 5) == "\xFF\xFF\xFF\xFF\x6A") {
echo '<FONT COLOR=lime>Online</FONT>';
} else {
echo '<FONT COLOR=red>Offline</FONT>';
}
socket_close($socket);
?>`
Bah! this code blocks don't really work fine! :(
Anyway, I use this code everyday for my server, hope it works fine for you.
Two choices off the top of my head:
fsockopen:
http://www.phptoys.com/e107_plugins/content/content.php?content.41
Net_Ping Pear package:
http://www.codediesel.com/php/ping-a-server-using-php/
Using IP of the CS server.
Get ClanManager and it has modules to check status of CS server.
This is the file with examples talking with CS servers,
http://clanmanager.cvs.sourceforge.net/viewvc/clanmanager/ocm/server/counterstrike.inc?revision=1.3&view=markup
You can also do what's known as a TCP ping. Basically, you just connect then disconnect without any communication. If the connection succeeds, you know the server is up, if it fails it's down. When I say server I mean the program you're interested in, not the physical server.
Here is PHP function to check if CS server is online:
function isCsServerOnline($server, $port = 27015)
{
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// timeout 1.5 seconds
$timeout = array('sec' => 1, 'usec' => 500000);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, $timeout);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout);
#socket_connect($socket, $server, $port);
#socket_write($socket, "\xFF\xFF\xFF\xFF\x69");
$isOnline = #socket_read($socket, 5) == "\xFF\xFF\xFF\xFF\x6A";
#socket_close($socket);
return $isOnline;
}
You can use it like this:
$server = 'some.cs.server';
$port = 27015;
$isOnline = isCsServerOnline($server, $port);
echo $isOnline ? 'Online' : 'Offline';

Categories