【问题标题】:How to run PHP Port Listener Script with WAMP not PHP CLI?如何使用 WAMP 而不是 PHP CLI 运行 PHP 端口侦听器脚本?
【发布时间】:2018-05-23 18:16:08
【问题描述】:

我希望我不要问垃圾邮件问题,因为我找不到任何关于它的问题。 我编写了一个套接字端口侦听器来侦听特定的 IP:PORT 使用 PHP 从 GPS 跟踪设备接收 TCP 数据包。 当我使用此命令运行脚本时:

$php.exe -q My/Script/Files/Path.PHP

一切正常,我的服务器端口监听器工作成功,我从设备接收数据,但是当我想用我的浏览器(wamp -> localhost)打开它时,据说

Warning: socket_bind(): unable to bind address [10049]: The requested address is not valid in its context.

我该如何解决这个问题? 最好的问候。

编辑一:
我的服务器脚本代码:

<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$address = '*.*.*.*'; //I put Here My System Local IPV4 Address (163.*.*.*)
$port = *****; //I put Here My Server Opened Port Number and I turned off the firewall to test

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, 1) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if ($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
 }    
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
 }
 $buf = trim($buf);
 $clientResponse = explode(',', $buf);
 foreach($clientResponse as $key=>$value) {
     echo $key . ' : ' . $value . "<br />";
}
socket_close($msgsock);
socket_close($sock);
?>

正如我之前所说:
当我使用 PHP CLI 运行此脚本时,一切正常,我的设备发送 TCP 数据包,我的服务器接收 TCP 数据包并显示该信息。但是当我通过 wamp 使用本地 IP 127.0.0.1 运行它时,它给了我这个错误。

到这里:

  • 我有一个VPS服务器
  • 我有一个静态 IP(公共 IP)
  • 我有一个关闭防火墙的开放 TCP 端口
  • 我有本地 IP
  • 我有一个 IP:Port -> 127.0.0.1:80 的 wamp

编辑二:
我的脚本端口:41260

【问题讨论】:

  • 打开Path.PHP,告诉我们socket_bind()里面有什么。 IE:使用哪个地址绑定套接字?
  • 你使用了什么端口?

标签: php apache networking wamp php-socket


【解决方案1】:

我用谷歌搜索了一些关键字,并在 Apache 的官方网站上找到了这个文档:(Bind)。我决定将结果分享给其他用户。

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 2015-06-24
    • 2020-10-09
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2012-11-20
    • 2011-10-18
    • 1970-01-01
    相关资源
    最近更新 更多