Last active
May 23, 2021 20:41
-
-
Save timmyRS/3760aa3cfbef822bac57fbccde73c119 to your computer and use it in GitHub Desktop.
Minecraft LAN world sender & receiver in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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