为什么 TCP 可以工作,而 UDP 却不能?

发布于 2024-07-15 22:22:57 字数 2245 浏览 5 评论 0原文

代码:

<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';
$port = 11100;

if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    do {
    $out = socket_read($msgsock, 2048);

    if (!empty($out)) {
        if ($out == 'quit') {
            break;
        }
        elseif ($out == 'shutdown') {
            socket_write($msgsock, 'plc down', 8);
            socket_close($msgsock);
            break 2;
        }
        else {
            switch ($out) {
                case "KABBE": $response = "Kabbe te!"; break;
                case "SZOPJ": $response = "Szopjal te!"; break;
                default: $response = "Ismeretlen parancs";
            }
            socket_write($msgsock, $response, strlen($response));
            break;
        }
    }
    } while (true);
socket_close($msgsock);
} while (true);

socket_close($sock);
?>

它适用于 TCP:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

但适用于 UDP 它不起作用:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

错误:

警告:socket_listen() [function.socket-listen]:无法侦听套接字 [0]:所引用的对象类型不支持尝试的操作。 在 C:\wamp\www\socket\socket.php 第 22 行 socket_listen() 失败:原因:引用的对象类型不支持尝试的操作。

警告:socket_accept() [function.socket-accept]:无法接受传入连接[0]:所引用的对象类型不支持尝试的操作。 在 C:\wamp\www\socket\socket.php 第 27 行 socket_accept() 失败:原因:引用的对象类型不支持尝试的操作。

The code:

<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';
$port = 11100;

if (($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    do {
    $out = socket_read($msgsock, 2048);

    if (!empty($out)) {
        if ($out == 'quit') {
            break;
        }
        elseif ($out == 'shutdown') {
            socket_write($msgsock, 'plc down', 8);
            socket_close($msgsock);
            break 2;
        }
        else {
            switch ($out) {
                case "KABBE": $response = "Kabbe te!"; break;
                case "SZOPJ": $response = "Szopjal te!"; break;
                default: $response = "Ismeretlen parancs";
            }
            socket_write($msgsock, $response, strlen($response));
            break;
        }
    }
    } while (true);
socket_close($msgsock);
} while (true);

socket_close($sock);
?>

It works with TCP:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

but with UDP it's not working:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

The errors:

Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: The attempted operation is not supported for the type of object referenced. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: The attempted operation is not supported for the type of object referenced.

Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: The attempted operation is not supported for the type of object referenced. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: The attempted operation is not supported for the type of object referenced.

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

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

发布评论

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

评论(2

分開簡單 2024-07-22 22:22:57

因为TCP是面向连接的,而UDP不是,并且UDP套接字有不同的API。 看看 socket_recvfromsocket_sendto

Because TCP is connection oriented and UDP is not, and there are different APIs for UDP sockets. Have a look at socket_recvfrom and socket_sendto.

坏尐絯℡ 2024-07-22 22:22:57

我通过将 Growl 类从 编辑为 来修复

 socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);

 socket_sendto($sck, $data, strlen($data), 0x0, $this->address, $this->port);

I've fixed it by editing Growl class from

 socket_sendto($sck, $data, strlen($data), 0x100, $this->address, $this->port);

to

 socket_sendto($sck, $data, strlen($data), 0x0, $this->address, $this->port);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文