PHP socket help needed urgent
Posted: Mon Jun 22, 2009 3:50 am
Dear all,
I have been using php script for developing my web server. Basically this server listens for client socket and accept incoming messages and return back to the client. The below is the code . The problem is , when the script executes socket_accept() it blocks the code and hence I am unable to print the echo values before that. The page remains blank. How do i overcome it.. I want the values to be printed in the page and later on the socket_accept can go for blocking how do i do that. what is the problem in this code pls reply
I have been using php script for developing my web server. Basically this server listens for client socket and accept incoming messages and return back to the client. The below is the code . The problem is , when the script executes socket_accept() it blocks the code and hence I am unable to print the echo values before that. The page remains blank. How do i overcome it.. I want the values to be printed in the page and later on the socket_accept can go for blocking how do i do that. what is the problem in this code pls reply
Code: Select all
<?
// set some variables
$host = "xx.xx.xx.xx";
echo "<br />"; echo "Host :";
echo $host; echo " @ ";
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create
socket\n");
$i = 1;
while($i == 1)
{
// bind socket to port
$result = socket_bind($socket, $host, 0);
$errorcode = socket_last_error();
if ($errorcode != 0)
{
$i = 1;
}
else
{
$i = 0;
}
}
socket_getsockname($socket, $host, $socket_port);
echo $socket_port;
// start listening for connections
$result = socket_listen($socket, SOMAXCONN) or die("Could not set up socket
listener\n");
echo "<br />";echo "Listening Socket Status:".$result;
// 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,PHP_NORMAL_READ) or die("Could not read input\n");
// clean up input string
$input = trim($input);
echo "<br />";echo "Recieved Input : ".$input;
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
echo "<br />";echo "Output From the Server : ".$output;
// close sockets
socket_close($spawn);
socket_close($socket);
?>