PHP:接收UDP广播

发布于 2025-01-09 18:53:06 字数 1201 浏览 0 评论 0原文

目前我正在尝试使用 PHP 接收 UDP 广播。这个话题在互联网上很难找到,如果没有真正的解决方案,我就能成功。 这是我当前的代码,如何设置服务器套接字来接收 UDP 广播:

        $context = stream_context_create([
            'socket' => [
                \SO_BROADCAST => 1,
                'so_broadcast' => 1,
            ],
        ]);
        $socket = @\stream_socket_server($address, $errno, $errstr, \STREAM_SERVER_BIND, $context);

因为我不确定是否必须使用常量 \SO_BROADCAST 或字符串 'so_broadcast ' 我在这个例子中列出了两者。当然,我已经尝试了所有不同的变化。这里的文档实际上非常清楚: https:// www.php.net/manual/en/context.socket.php#context.socket.so_broadcast 我仍然收不到广播。我目前正在通过 NC 进行测试:

$ echo 'HELLO' | nc -u -v -b 10.88.0.255 20000

通过 tcpdump 我还可以在服务器接口上看到广播,但不幸的是它没有到​​达我的套接字?如果我直接向服务器 IP 发送数据包,它就会到达并且我可以处理它。

还有其他人对我有想法吗?

问候,沃尔克。

编辑:

一个简单的上下文参数反向检查显示:

var_dump(stream_context_get_options($context));
# array(1) {
#  ["socket"]=>
#  array(1) {
#    ["so_broadcast"]=>
#    int(1)
#  }
#}

所以我认为 string 选项是正确的决定......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉鱼一梦 2025-01-16 18:53:06

答案很简单,检查防火墙设置。这是我的问题,我的本地防火墙阻止广播到 255.255.255.255。

以下是在专用接口 ep1 上接收 DHCP 广播数据包的完整示例代码:

<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, 'ep1');
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

socket_bind($socket, '255.255.255.255', 67);
while (1) {
    if ($src = @socket_recv($socket, $data, 9999, 0)) {
        echo $data . PHP_EOL;
    }
}

Answer is easy, check the firewall settings. this was my problem, my local firewall block broadcasts to 255.255.255.255.

here is an complete sample code to receive DHCP broadcast packets on dedicated interface ep1:

<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, 'ep1');
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

socket_bind($socket, '255.255.255.255', 67);
while (1) {
    if ($src = @socket_recv($socket, $data, 9999, 0)) {
        echo $data . PHP_EOL;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文