我正在尝试使用php socket连接到服务器。我使用以下PHP代码,工作正常:
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$host = "192.168.1.2";
$port=15000;
settype($buffer,"string");
//$buffer="0 MAIN VERSION\0";
$len= strlen($buffer);
socket_connect($sock, $host, $port);
socket_sendto($sock, $buffer, $len, 0 , $host, $port);
echo( socket_read($sock, 65535) );
socket_close($sock);
?>
我想创建一个表单并从该表单输入$ buffer变量然后处理它。 问题是套接字连接没有连接,也没有收到任何错误。 这是表单代码:
<html>
<head>
<title>TEST CMD</title>
<form action="send_cmd.php" method="POST" >
COMMAND:<input type="text" name="cmd" size="70">
<input type="submit" value="send cmd">
</form>
</head>
这是send_cmd.php:
<?php
settype($buffer,"string");
$buffer= $_POST["cmd"];
$len= strlen($buffer);
ECHO $buffer;
ECHO $len;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('ERROR');
$host = "192.168.1.2";
$port=15000;
socket_connect($sock, $host, $port) or die('ERROR');;
socket_sendto($sock, $buffer, $len, 0 , $host, $port);
echo $buffer;
echo( "ANSWER: <P>".socket_read($sock, 65535) );
socket_close($sock);
?>
有什么问题?应该工作,但事实并非如此。谢谢!
答案 0 :(得分:1)
不是答案,但评论的时间太长了:
也许更多的调试输出可以解决问题:
什么
<?php
ini_set('display_errors', true); error_reporting(E_ALL); // for debugging purposes only
$buffer= $_POST['cmd'];
echo '<pre>buffer: '; var_dump($buffer); echo "</pre>\r\n"; flush();
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('ERROR');
$host = "192.168.1.2";
$port=15000;
socket_connect($sock, $host, $port) or die('ERROR');
$cb = socket_sendto($sock, $buffer, $len, 0 , $host, $port);
echo '<pre>#sent: '; var_dump($cb); echo "</pre>\r\n"; flush();
$response = socket_read($sock, 65535);
echo '<pre>response'; var_dump($response); echo "</pre>\r\n"; flush();
打印?