【问题标题】:php Socket Socket_bind() unable to bind address [10013]php Socket Socket_bind() 无法绑定地址 [10013]
【发布时间】:2019-07-25 21:03:27
【问题描述】:

我的情况:

  • 我有一个程序在端口 (4448) 上发送来自某些机器的大量数据。
  • 我可以通过 telnet 连接检索这些数据。

远程登录步骤:

  • 以 cmd 作为 ADMIN 启动
  • 写入 telnet 并发送
  • 写“open localhost 4448”并发送
  • 写“GET xx,yy”
  • 在此之后,我只是在现场阅读屏幕信息

xx 和 yy 是标识机器的数字

现在让我们继续 php 方面,我有我的 index.php,但是当我执行套接字绑定时,它给了我一个错误(在代码下报告)和关于代码,这来自:http://php.net/manual/en/sockets.examples.php 及其第一个示例,我也阅读并尝试了这个解决方案https://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP,它使用了 2 个 php 文件,在这两种情况下我都会遇到同样的错误......

<?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 = 4448;

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;
    }
    /* Send instructions. */
    $msg = "GET xx,yy";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.\n";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf\n";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>

这是我得到的错误:

警告:socket_bind():无法绑定地址 [10013]:尝试被 以访问权限禁止的方式访问套接字

我已经找过管理员权限了,我觉得没问题。

额外信息:我正在使用 PHP 7,我的 Web 服务器是 Xampp 包中的 Apache。

【问题讨论】:

    标签: php sockets telnet


    【解决方案1】:

    可能另一个程序已经在使用该端口。尝试使用不同的端口。如果可行,您可以尝试使用端口查找进程并终止它。来自 sysinternals 的这个工具可能会有所帮助:https://docs.microsoft.com/en-us/sysinternals/downloads/tcpview

    如果使用其他端口也失败,则可能是您的防火墙不允许绑定。尝试暂时禁用防火墙。

    【讨论】:

    • 我已经尝试过使用其他端口号 25 个不同的端口,而且它们都肯定是免费的......而且我也禁用了防火墙
    猜你喜欢
    • 2018-05-11
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 2023-03-31
    • 2012-05-30
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多