套接字不工作PHP

时间:2009-03-29 20:22:49

标签: 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 = '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);
?>

现在错误

Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\wamp\www\socket\socket.php on line 18
socket_bind() failed: reason: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Warning: socket_listen() [function.socket-listen]: unable to listen on socket [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 22
socket_listen() failed: reason: An invalid argument was supplied.
Warning: socket_accept() [function.socket-accept]: unable to accept incoming connection [0]: An invalid argument was supplied. in C:\wamp\www\socket\socket.php on line 27
socket_accept() failed: reason: An invalid argument was supplied. 

我在谷歌搜索但没什么用处。 有什么问题?

4 个答案:

答案 0 :(得分:3)

PHP还提供stream_socket_server和其他stream_socket_ *函数 我发现这些更适合开发人员。

来自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);
}

答案 1 :(得分:1)

'localhost'不是有效地址,因为socket_bind不接受DNS名称,使用等效的IP地址'127.0.0.1'。

More info

答案 2 :(得分:0)

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

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

在Windows上(您似乎正在处理),您可以从命令行中看到打开的套接字列表:

netstat -an

如果您想知道哪些进程正在侦听这些端口,请尝试以下方法:

netstat -ban

答案 3 :(得分:0)

在绑定之前使用此代码:

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

供参考http://www.php.net/manual/en/function.socket-bind.php

您还可以查看http://www.php.net/manual/en/function.socket-set-option.php了解详情