Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么TCP工作,而UDP不工作?_Php_Sockets_Tcp_Udp - Fatal编程技术网

Php 为什么TCP工作,而UDP不工作?

Php 为什么TCP工作,而UDP不工作?,php,sockets,tcp,udp,Php,Sockets,Tcp,Udp,守则: <?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.

守则:

<?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);
?>
但UDP不起作用:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
错误:

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

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


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

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

我已经通过编辑来自的咆哮类修复了它

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


我已经通过编辑来自的咆哮类修复了它

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

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