我的 html 页面中有三个按钮连接到 php 脚本,它们使用套接字连接向另一个应用程序发送命令。
1) 当应用程序接收命令并且 xampp 服务器在本地主机上运行时,它工作正常,但是当我尝试在网络上发送命令时,它有时有效,有时无效。
2) 这背后的原因可能是什么。
button1 的代码
<?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);
?>
最佳答案
检查此代码是否有错误,可能是超时?
if ($socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg");
}
关于javascript - PHP Socket 读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498408/