hi,
i have a client and a server using uniform server and im using sockets to connect them.
i would like to be able to input information from say a form on the client side, send it to the server, the server would then input this data and send it back as an html table to view on the client side. how would i go about this?
i have the server sending back basic html but not what is typed into the form.
i have this code for the server

<?
// set some variables
$host = "localhost";
$port = 10010;


// don't timeout!
set_time_limit(0);


// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");


// bind socket to port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");


// start listening for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");


// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");


// read client input
$input = socket_read($spawn, 1024);


// clean up input string
$input = trim($input);


//$output = $input . "\n";


//$output = $input;


$output = "<table colour = &quot; red &quot; border = &quot; 1 &quot;>
<caption> <strong>Table Caption</strong></caption>
<thead> <tr> <th> head col 1 </th> <th> $input  </th>
</tr> </thead> <tfoot> <tr>
<th> Total </th> <th> $input </th>
</tr>
</tfoot>
<tbody> <tr>
<td> Sausage</td> <td> £4.21 </td> </tr>
<tr> <td> Mash </td> <td> £1.13</td>
</tr>
</tbody>
</table>";


socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");


// close sockets
socket_close($spawn);
socket_close($socket);
?>


and this code client side


<html>
<head>
</head>
<body>
<?php
if(isset($_POST)) {
$str = $_POST["fname"];
do_it($str);
}
?>
<form action="<?=$_SERVER;?>" method="post">
<input type="text" name="fname" value="" />
<br>
<input type="submit" name="submit" value="Click Me">
</form>


<?
function do_it($str)
{
// set some variables
$host = "localhost";
$port = 10010;
$ret;


// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$ret = socket_connect($socket, $host, $port);
$str = $str . "\n";
socket_write($socket, $str);
$input = socket_read($socket,1024);
$input = trim($input);


echo $input;


socket_close($socket);


}
?>


</body
</html>

thanks

Edited 3 Years Ago by happygeek: fixed formatting

Try to telnet into your socket to see if it is working as expected or not.

telnet host port
type something
see response


It would probably kill the listening socket on the first key press, since its just listening for the first read and telnet will send as you type (unless you specify not to).

But you should get an idea of how its working.

This article has been dead for over six months. Start a new discussion instead.