在这里,我希望将消息从客户端传递到服务器,并通过服务器程序将响应写回客户端。实际上,我将数据库保存在服务器端,并尝试根据客户端输入将数据发送回客户端。从客户端到服务器的发送部分并从服务器检索数据是可以的。我想要一些想法作为回复回信给客户
这是我的服务器程序
<html>
<head>
</head>
<body>
<?php
//socket_close($sock);
$isread=false;
$address="127.0.0.1";
$port="1332";
$sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Socket creation failed..!");
socket_bind($sock,$address,$port) or die("Socket binding failed..!");
socket_listen($sock) or die("Socket listen failed..!");
$accept=socket_accept($sock) or die("Socket accept failed..!");
$read=socket_read($accept,1024);
//echo $read;
if($read)
$isread=true;
$ar=array();
$ar=explode("@",$read);
print_r($ar);
if($isread==true)
getData('testdata','student',$accept,$ar[0],$ar[1]);
function getData($dbName,$tblName,$acc,$control,$value)
{
mysql_connect('localhost','root','') or die("Connection failed..!");
mysql_select_db($dbName) or die("Connection failed..!");
$cmd="SELECT * FROM $tblName WHERE '$control'='$value'";
$query=mysql_query($cmd) or die("Query execution failed..! ".mysql_error());
$rows=mysql_num_rows($query);
$cols=mysql_num_fields($query);
for($i=0;$i<$rows;$i++)
{
$tbl=mysql_fetch_array($query);
for($j=0;$j<$cols;$j++)
{
socket_write($acc,$tbl[0]+"@");
}
echo "<br />";
}
}
socket_close($sock);
?>
</body>
</html>
这是我的客户程序
<html>
<head></head>
<body>
<form action="myclient.php" method="POST">
<select name="data">
<option value="RegNo">RegNo</option>
<option value="Name">Name</option>
</select>
<input type="text" name="mytext"></input>
<input type="Submit" name="btn"></input>
</form>
</body>
</html>
<?php
$iswrite=false;
if(isset($_POST['btn']))
{
$d=$_POST['data'];
$s=$_POST['mytext'];
$address="127.0.0.1";
$port="1332";
if(isset($d) && isset($s))
{
$sock=socket_create(AF_INET,SOCK_STREAM,0) or die("Cannot create socket");
$con=socket_connect($sock,$address,$port) or die("Cannot connect to socket");
socket_write($sock,$d);
socket_write($sock,"@");
$write=socket_write($sock,$s);
if($write)
$iswrite=true;
if($iswrite)
{
$read=socket_read($sock,1024);
echo $read;
}
socket_close($sock);
}
}
请给我一个想法。