Hello,

I'm just getting my feet wet with a Dev Shed sockets tutorial (http://www.devshed.com/Server_Side/PHP/SocketProgramming/page5.html). I am having a little problem with one of the tutorial code samples, which should socket_write some text back to the client--except it's not doing that. Here's the snippet:



// write a welcome message to the client
$welcome = "Roll up, roll up, to the greatest show on earth!\n? ";
socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send
connect string\n");

// keep looping and looking for client input
do
{
// read client input
$input = socket_read($spawn, 1024, 1) 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) . "\n";
socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could
not write output\n");
echo "Sent output: " . trim($output) . "\n";
}
}

} while (true);



And the output to the Telnet client:
[Valhalla:~] rene% telnet 192.168.0.77 1234
Trying 192.168.0.77...
Connected to 192.168.0.77.
Escape character is '^]'.
Roll up, roll up, to the greatest show on earth!
? Hello, this is a test.
END
Connection closed by foreign host.
[Valhalla:~] rene%




Server output:
% /usr/local/php/bin/php -q socket3.php4
Waiting for connections...
Received connection request
Received input: Hello, this is a test.
Sent output: .tset a si siht ,olleH
Received input: END
Socket terminated
This is almost exactly the same code as that used in the very first example, with the obvious addition of debug messages and a "do-while" loop [Valhalla:~] rene%



According to the tutorial, the client should see the string reversed, but that's not happening... In the previous tutorial example it did reverse and output to the client, and the code looks identical, save for the do...while loop. Here's what the tutorial says should happen:




Here's the output of a sample session:
$ telnet 192.168.1.99 1234
Trying 192.168.1.99...
Connected to medusa.
Escape character is '^]'.
Roll up, roll up, to the greatest show on earth!
? Look Ma...all backwards
sdrawkcab lla...aM kooL
? jack frost
tsorf kcaj
? END
Connection closed by foreign host.
And here are the corresponding debug messages generated on the server:
$ /usr/local/bin/php -q server.php
Waiting for connections...
Received connection request
Received input: Look Ma...all backwards
Sent output: sdrawkcab lla...aM kooL
Received input: jack frost
Sent output: tsorf kcaj
Received input: END
Socket terminated




Any ideas?


...Rene


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to