【问题标题】:PHP Socket read errorPHP Socket 读取错误
【发布时间】:2014-12-16 05:52:40
【问题描述】:

我的 html 页面中有三个按钮连接到 php 脚本,它们使用套接字连接将命令发送到另一个应用程序。

1) 当应用程序接收命令和 xampp 服务器在 localhost 上运行时,它工作正常,但是当我尝试在网络上发送命令时,它有时可以工作,有时不能。

2) 这背后的原因是什么。

按钮 1 的代码

<?php
// Fill up array with names
$q=$_GET["q"];



$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["vz"];
$port=6100;
$buffer=$q ."\0";
$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);


$buffer='COMMAND HERE';
$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

按钮 2 的代码

?php


$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["x"];
$port=6100;
$buffer='COMMAND HERE';

$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);



$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

按钮 3 的代码

<?php


$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = $_GET["x"];
$port=6100;
$buffer='COMMAND HERE';

$len= strlen($buffer);

socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);



$len= strlen($buffer);


socket_sendto($sock, $buffer, $len, 0 , $host, $port);

echo( socket_read($sock, 65535) );

socket_close($sock);


?>

【问题讨论】:

    标签: javascript php html


    【解决方案1】:

    检查此代码是否有错误,可能是超时?

    if ($socket === false) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
    
        die("Couldn't create socket: [$errorcode] $errormsg");
    }
    

    【讨论】:

      【解决方案2】:

      以下代码在 chrome 和最新版本的 Internet Explorer 上运行良好

      【讨论】:

      • 我的意思是说,当 IE 更新到最新版本时,我粘贴的相同代码运行良好
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      相关资源
      最近更新 更多