Skip to content

Instantly share code, notes, and snippets.

@flotwig
Last active July 1, 2021 13:54
Show Gist options
  • Save flotwig/5795159 to your computer and use it in GitHub Desktop.
Save flotwig/5795159 to your computer and use it in GitHub Desktop.
Minecraft Server List Properties Example A concise, over-commented, efficiently-coded example for pinging a Minecraft server and retrieving its status. No error checking.
<?php
function pingMCServer($server,$port=25565,$timeout=2){
$socket=socket_create(AF_INET,SOCK_STREAM,getprotobyname('tcp')); // set up socket holder
socket_connect($socket,$server,$port); // connect to minecraft server on port 25565
socket_send($socket,chr(254).chr(1),2,null); // send 0xFE 01 -- tells the server we want pinglist info
socket_recv($socket,$buf,3,null); // first 3 bytes indicate the len of the reply. not necessary but i'm not one for hacky socket read loops
$buf=substr($buf,1,2); // always pads it with 0xFF to indicate an EOF message
$len=unpack('n',$buf); // gives us 1/2 the length of the reply
socket_recv($socket,$buf,$len[1]*2,null); // read $len*2 bytes and hang u[
$data=explode(chr(0).chr(0),$buf); // explode on nul-dubs
array_shift($data); // remove separator char
return $data; // boom sucka
}
@timmyRS
Copy link

timmyRS commented Oct 25, 2018

$timeout is never used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment