duancai7568 2019-03-04 14:21
浏览 660

php Socket Socket_bind()无法绑定地址[10013]

My situation:

  • I have a program which send on a port (4448) a lot of data from some machines.
  • I can retrieve that datas from a telnet connection.

telnet steps:

  • launch as cmd as ADMIN
  • write telnet and send
  • write "open localhost 4448" and send
  • write "GET xx,yy"
  • after this i just read on screen info in live

xx and yy are numbers which identifies the machine

Now lets go on php side I have my index.php but when I execute socket bind it gives me back an error (reported under the code) and about the code, this come from: http://php.net/manual/en/sockets.examples.php and its the first example, i read and tried this solution also https://www.codeproject.com/Tips/418814/Socket-Programming-in-PHP which use 2 php files, in both cases i get the same error...

<?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()) . "
";
}

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

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

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
        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)) . "
";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.
";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf
";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>

and this is the error i get:

Warning: socket_bind(): unable to bind address [10013]: An attempt was made to access a socket in a way forbidden by its access permissions

I have already looked for administrator privileges and I think its all right.

Extra info: I am working with PHP 7, my web server is Apache in Xampp package.

  • 写回答

1条回答 默认 最新

  • doudou1438 2019-03-04 14:34
    关注

    It's possible another program is already using that port. Try using a different port. If that works you can try finding the process using the port and terminating it. This tool from sysinternals may help: https://docs.microsoft.com/en-us/sysinternals/downloads/tcpview

    If using a different port also fails, a possiblity is that you have a firewall that doesn't allow bind. Try disabling your firewall temporarily.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)