-
-
Save DaveRandom/30867d6702361c0e6e27 to your computer and use it in GitHub Desktop.
Stream bcast
This file contains 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 | |
$port = 12345; | |
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); | |
socket_bind($sock, '0.0.0.0', $port); | |
while (1) { | |
$r = [$sock]; | |
$w = $e = $t = null; | |
socket_select($r, $w, $e, $t); | |
socket_recvfrom($sock, $buf, 1024, 0, $name, $port); | |
echo "Received from $name:$port: $buf\n"; | |
} |
This file contains 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 | |
$message = 'This is a broadcast message!'; | |
$addr = '192.168.49.255'; | |
$port = 12345; | |
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); | |
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); | |
socket_sendto($sock, $message, strlen($message), 0, $addr, $port); | |
socket_close($sock); | |
echo 'sent'; |
This file contains 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 | |
$port = 12345; | |
$ctx = stream_context_create([ | |
'socket' => [ | |
'broadcast' => true, | |
] | |
]); | |
$sock = stream_socket_server("udp://0.0.0.0:$port", $errNo, $errStr, STREAM_SERVER_BIND, $ctx); | |
while (1) { | |
$r = [$sock]; | |
$w = $e = $t = null; | |
stream_select($r, $w, $e, $t); | |
$buf = stream_socket_recvfrom($sock, 1024, 0, $addr); | |
echo "Received from $addr: $buf\n"; | |
} |
This file contains 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 | |
$message = 'This is a broadcast message!'; | |
$addr = '192.168.49.255'; | |
$port = 12345; | |
$ctx = stream_context_create([ | |
'socket' => [ | |
'broadcast' => true, | |
] | |
]); | |
$sock = stream_socket_client("udp://$addr:$port", $errNo, $errStr, 2, STREAM_CLIENT_CONNECT, $ctx); | |
stream_socket_sendto($sock, $message, 0); | |
echo 'sent'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment