Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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
Socket无法运行PHP_Php_Sockets - Fatal编程技术网

Socket无法运行PHP

Socket无法运行PHP,php,sockets,Php,Sockets,这是我的密码 <?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 = 'local

这是我的密码

<?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 = 'localhost';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === 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);
?>

这意味着您的计算机在该端口上已经有一个打开的套接字

尝试切换到另一个未使用的端口

在Windows上(这似乎是您正在使用的),您可以从命令行看到打开的套接字列表:

netstat -an
如果您想知道哪些进程正在侦听这些端口,请尝试以下操作:

netstat -ban

这意味着您的计算机在该端口上已经有一个打开的套接字

尝试切换到另一个未使用的端口

在Windows上(这似乎是您正在使用的),您可以从命令行看到打开的套接字列表:

netstat -an
如果您想知道哪些进程正在侦听这些端口,请尝试以下操作:

netstat -ban

“localhost”不是有效地址,因为套接字绑定不接受DNS名称,请使用等效的IP地址“127.0.0.1”


'localhost'不是有效地址,因为套接字绑定不接受DNS名称,请使用等效的IP地址'127.0.0.1'

PHP还提供流和其他套接字函数。
我发现这些对开发人员更友好

php.net中的示例代码:

$socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)<br />\n";
} else {
  while ($conn = stream_socket_accept($socket)) {
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    fclose($conn);
  }
  fclose($socket);
}
$socket=stream\u socket\u服务器(“tcp://localhost:8000“,$errno,$errstr);
如果(!$socket){
回显“$errstr($errno)
\n”; }否则{ 而($conn=stream\u socket\u accept($socket)){ fwrite($conn,‘当地时间是’.date('n/j/Y g:ia')。“\n”); fclose($康涅狄格州); } fclose($socket); }
PHP还提供了流和其他套接字函数。
我发现这些对开发人员更友好

php.net中的示例代码:

$socket = stream_socket_server("tcp://localhost:8000", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)<br />\n";
} else {
  while ($conn = stream_socket_accept($socket)) {
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    fclose($conn);
  }
  fclose($socket);
}
$socket=stream\u socket\u服务器(“tcp://localhost:8000“,$errno,$errstr);
如果(!$socket){
回显“$errstr($errno)
\n”; }否则{ 而($conn=stream\u socket\u accept($socket)){ fwrite($conn,‘当地时间是’.date('n/j/Y g:ia')。“\n”); fclose($康涅狄格州); } fclose($socket); }
绑定前使用此代码:

if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($socket));
    exit;
}
供参考


您还可以查看详细信息

在绑定前使用此代码:

if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    echo socket_strerror(socket_last_error($socket));
    exit;
}
供参考

您还可以查看详细信息