通过端口11300在本地主机(127.0.0.1)上通过PHP使用telnet
我无法通过端口11300在本地主机(127.0.0.1)上通过PHP使用telnet 我以前没有用过这个,所以帮助会很好 代码如下:通过端口11300在本地主机(127.0.0.1)上通过PHP使用telnet,php,localhost,telnet,Php,Localhost,Telnet,我无法通过端口11300在本地主机(127.0.0.1)上通过PHP使用telnet 我以前没有用过这个,所以帮助会很好 代码如下: function sendToSocket($host, $port, $out){ if(!function_exists('fsockopen')) return 'f() doesnt exist !'; $response = ""; $fp = fsockopen($host, $port, $errno, $e
function sendToSocket($host, $port, $out){
if(!function_exists('fsockopen'))
return 'f() doesnt exist !';
$response = "";
$fp = fsockopen($host, $port, $errno, $errstr);
if(!$fp){
$response .= "$errstr ($errno)<br/>";
}else{
fwrite($fp, $out);
$response .= fgets($fp);
fclose($fp);
}
if($response)
return $response;
else
return "ERROR";
}
echo sendToSocket('127.0.0.1', 11300, 'stats');
函数sendToSocket($host、$port、$out){
如果(!function_存在('fsockopen'))
返回“f()不存在!”;
$response=“”;
$fp=fsockopen($host、$port、$errno、$errstr);
如果(!$fp){
$response.=“$errstr($errno)
”;
}否则{
fwrite($fp,$out);
$response.=fgets($fp);
fclose($fp);
}
如果($答复)
返回$response;
其他的
返回“错误”;
}
echo sendToSocket('127.0.0.1',11300,'stats');
我得到了“错误”,这意味着fgets($fp);不适合我
在命令行中键入:telnet 127.0.0.1 11300时,一切正常,因此我可以键入命令“stats”以获得结果。我正在使用Ubuntu
错在哪里?
如何在命令行中获得类似结果的结果?此脚本仅从响应中读取一行
$response .= fgets($fp);
必须用类似的东西来代替
while (!feof($fp)) {
$response .= fgets($fp);
}
我将代码更正为:
echo sendToSocket('127.0.0.1',11300,“统计信息\r\n”)
一切正常,一切正常
注意:如果您将单引号作为输出发送,则代码不起作用。可能需要发送
\n
或类似的内容,以便实际告诉另一端您已进行了一些输入?我更正为:echo sendToSocket('127.0.0.1',11300,“stats\r\n”);它正在工作,一切正常,非常感谢。请将其添加为下面的答案。然后,您可以选择它作为答案,该答案将标记此问题已解决。谢谢我删除了while循环,因为问题是一样的。