现在用php连接socket后得到的buffer可以打印出来,网页上的显示如下面所示:
接收到的数据如下所示:
response was:1
response was:1
response was:1
response was:1
response was:1
response was:1
response was:1
现在是向把这个buffer填写到一个文本框中,并一直刷新,不知道怎么实现,请大神们帮助。下面是我的代码!
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<form action="" method="post" name="form1" >
<input type="text" size="12" name="t1" /><br>
<input type="text" size="12" name="t2"/><br/>
<input type="submit" name="button" value="连接"/><br/>
<input type="submit" name="button2" value="断开连接"/>
</form>
<?php
if(isset($_POST['button'])){
echo "接收到的数据如下所示:";
echo "<br/>";
$port=$_POST["t2"];
$host=$_POST["t1"];
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket\n"); // 创建一个Socket
$connection = socket_connect($socket, $host, $port) or die("Could not connet server\n"); // 连接
//$connection = socket_connect($socket, "127.0.0.1", 7777) or die("Could not connet server\n"); // 连接
socket_write($socket, "hello socket") or die("Write failed\n"); // 数据传送 向服务器发送消息
ob_end_flush();
//echo str_pad(" ", 256);
while($buffer=socket_read($socket,1,PHP_NORMAL_READ)){
echo "response was:".$buffer."\n";
echo "<br/>";
flush();
}
socket_close($socket);
}
if(isset($_POST['button2'])){
socket_close($socket);
}
?>
</body>
------解决办法--------------------
response was:<input type="text" size="12" name="t3"/><br/>
.
.
.
.
echo "<script type='text/javascript'>document.form1.t3.value='$buffer';</script>";