Skip to content

Instantly share code, notes, and snippets.

@timmyRS
Last active May 23, 2021 20:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmyRS/3760aa3cfbef822bac57fbccde73c119 to your computer and use it in GitHub Desktop.
Save timmyRS/3760aa3cfbef822bac57fbccde73c119 to your computer and use it in GitHub Desktop.
Minecraft LAN world sender & receiver in PHP
<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Failed to create socket.\n");
socket_set_option($socket, IPPROTO_IP, MCAST_JOIN_GROUP, [
"group" => "224.0.2.60",
"interface" => 0
]);
socket_bind($socket, "0.0.0.0", 4445) or die("Failed to bind.\n");
$msg_regex = '/^\[MOTD\]([^\[\]]+)\[\/MOTD\]\[AD\]([0-9]{4,5})\[\/AD\]$/';
while(true)
{
socket_recvfrom($socket, $msg, 1024, 0, $from, $port);
if(preg_match($msg_regex, $msg, $matches) === 1)
{
echo $matches[1]." available on $from:".$matches[2]."\n";
}
}
<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Failed to create socket.\n");
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
$msg = "[MOTD]Hello, I'm definitely a LAN world![/MOTD][AD]25565[/AD]";
socket_sendto($socket, $msg, strlen($msg), 0, "224.0.2.60", 4445);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment