Skip to content

Instantly share code, notes, and snippets.

@timmyRS timmyRS/mcast_recv.php
Last active Sep 15, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.