Currently I am trying to receive UDP broadcasts with PHP. The topic is very rare to find on the internet and if there are no real solutions that would have led me to success. Here is my current code how I set up my server-socket to receive the UDP-broadcasts:
$context = stream_context_create([
'socket' => [
\SO_BROADCAST => 1,
'so_broadcast' => 1,
],
]);
$socket = @\stream_socket_server($address, $errno, $errstr, \STREAM_SERVER_BIND, $context);
since i'm not sure if i have to use the constant \SO_BROADCAST
or the string 'so_broadcast'
i've listed both in this example. of course i've tried everything in different variations. the documentation is actually quite clear here: https://www.php.net/manual/en/context.socket.php#context.socket.so_broadcast
still i can't receive a broadcast. i'm currently testing this via NC:
$ echo 'HELLO' | nc -u -v -b 10.88.0.255 20000
via tcpdump
i can also see the broadcast on the server interface, but unfortunately it does not arrive at my socket? if i send a packet directly to the server ip it arrives and i can process it.
anyone else have an idea for me?
regards, volker.
EDIT:
an simple context params reverse check shows:
var_dump(stream_context_get_options($context));
# array(1) {
# ["socket"]=>
# array(1) {
# ["so_broadcast"]=>
# int(1)
# }
#}
so i think the option as string
is the right decision...
CodePudding user response:
Answer is easy, check the firewall settings. this was my problem, my local firewall block broadcasts to 255.255.255.255.