Jump to content

Archived

This topic is now archived and is closed to further replies.

Kai_soze

PHP Sockets.

Recommended Posts

Basically i coded this yesterday

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Radio Button Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<strong>Table Creater:</strong>
<br />
<p>
<strong>Table Heading</strong>
<input type="text" name="message" size="30" />
<p>
    <table border="0" width="300" cellpadding="5" cellspacing="0">
        <tr>
            <td width="50%">
             Rows
             <select name="rows">
                 <?php
$maxRows = 10;
for ($i = 1; $i <= $maxRows; $i++)
{
echo '<option value="' . $i . '">' . $i . '</option>';
}
?>
                </select>
            </td>
            <td width="50%">
             Columns
             <select name="cols">
                 <?php
$maxCols = 10;
for ($i = 1; $i <= $maxCols; $i++)
{
echo '<option value="' . $i . '">' . $i . '</option>';
}
?>
                </select>
            </td>
        </tr>
    </table>
<p>
<input type="submit" value="Submit" />
</form>


<p> </p>


<?php


if (isset($_POST['rows']) && isset($_POST['cols']))
{
// table start
echo '<table border="1" width="100%" cellpadding="5" cellspacing="0">';
// loop for each row
for ($i = 0; $i < $_POST['rows']; $i++)
{
// row start
echo '<tr>';
// output columns for the row
for ($x = 0; $x < $_POST['cols']; $x++)
{
echo '<td align="center">DATA</td>';
}
// row finish
echo '</tr>';
}
}


?>


</body>
</html>

 

As you can see its all done server side on the fly is it atall possible for it to be done via client asking for the information via sockets. e.g. Coded this yesterday also.

 

Server Socket Code

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

// 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) or die("Could not read input\n");

// decrypt back to orginal form
$input = str_split ($input);
$output = "";
foreach($input as $chr) {
$output.=chr(ord($chr)-1);
}

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

echo $output . "<br />\r\nThe first letter was: " . substr($output, 0, 1);

$indent =  substr($output, 0, 1);

$table2 = "<table border=1><tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>"; 

$table3 = "<table border=1><tr><td>Cell A</td><td>Cell B</td></tr></table>";


if ($indent == "T"){
echo "<br/> hello this is printing a table";
socket_write($spawn, "Table 1 ".$table2);
} else {
	socket_write($spawn, "please try again");
}

if ($indent == "B"){
echo "<br/> hello this is printing a table with Boarder";
socket_write($spawn, "Table 1 ".$table3);
} else {
	socket_write($spawn, "please try again");
}


//socket_write($spawn, "you have sent: ".$output);

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

 

Client Side

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>TESTING TCP SOCKET SERVER</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<?
// set some variables
$host = "localhost";
$port = 10081;
$mess = "";
$ret;



if (isset($_POST['message'])) {
$mess = $_POST['message'];

// encrypt message increment by 1
$input = str_split ($mess);
$output = "";
foreach($input as $chr) {
$output.=chr(ord($chr)+1);
}

        echo "Encryted Text:".$output."<br/>\n"; 
           
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");

$ret = socket_connect($socket, $host, $port);


//socket_write($socket, $mess );
socket_write($socket, $output );
sleep(1);

$message = socket_read($socket, 1024);
echo $message;

// close sockets

socket_close($socket);
}


?>

<h1>Enter your message here:</h1>
<form action="" method="post">
<input type="text" name="message" size="30" /><br />
<input type="submit" name="send" value="Send Value" />
</form>
</body>
</html>

 

No one can see to get that on the fly code and get it so the client can ask for it. Bascally all i need is for the client to have few drop down box saying how many rows and colums he wants. Goes to the server finds what he is asking for and posts the table back in html

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.