ok...i'm a total noob when it comes to sockets.
that said, i have the script below working with a flash client. i can send a message every 1/13 of a second and get a response within 80 ms. HOWEVER, i want my script to allow more than one client to connect at a time and i want the script to pass information between the two (more more clients).
i use this script as follows
1) run php from command line to create socket listen
2) connect via telnet from another machine
3) run flash client which also connects to same domain/port
can someone tell me why my script seems to accept a 2nd connection, but it doesn't ECHO anything when the second user connects? also, the 2nd client cannot send any messages through the socket....or that is to say the client doesn't respond.
here's the script:
<?
// don't timeout
set_time_limit (0);
// set some variables
$host = "199.227.90.72";
$port = 1234;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n" . socket_strerror(socket_last_error()));
// 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");
echo "Waiting for connections...\n";
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
echo "Received connection request\n";
// write a welcome message to the client
$welcome = "Roll up, roll up, to the greatest show on earth!\n? \0";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send
connect string\n");
do
{
//echo "loop START\n";
// read client input
$input = socket_read($spawn, 1024, PHP_BINARY_READ) or die("Could not read input\n");
if (trim($input) != "")
{
// echo "Received input: $input\n";
// if client requests session end
if (trim($input) == "END")
{
// close the child socket
// break out of loop
socket_close($spawn);
break;
}
// otherwise...
else
{
// reverse client input and send back
// $output = strrev($input) . "\0";
$output = trim($input) . "\0";
socket_write($spawn, $output . "? ", strlen($output)) or die("Could
not write output\n");
// echo "Sent output: " . trim($output) . "\n";
}
}
} while (true);
// close primary socket
socket_close($socket);
echo "Socket terminated\n";
?>