Mike
Visitor
|
Re:php / telnet connect to palabre - 2008/06/30 10:17
Hi all,
I figured out my problem.
The script is now working fine, here is the sniplet if you are interested:
| Code: |
<?php
header("Content-Type: text/html");
echo "Connecting to Palabre<br><br>";
/* connection data */
$service_host = "localhost";
$service_port = 2468;
/* create socket */
echo "creating socket...<br>";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() aborted: Reason: " . socket_strerror(socket_last_error()) . "<br><br>";
} else {
echo "socket_create() OK.<br><br>";
}
/* set socket receive timeout to 2 second */
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0));
echo "connecting to '$service_host' on port '$service_port' ...<br>";
$result = socket_connect($socket, $service_host, $service_port);
if ($result === false) {
echo "socket_connect() aborted: Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "<br><br>";
} else {
echo "socket_connect() OK.<br><br>";
}
//sending stuff here
echo "Send process<br>";
$out = "<connect nickname=\"Nickname\" />\0";
$in = '';
echo "SEND: $out <br>";
socket_write($socket, $out, strlen($out));
echo "RECV: ";
while ($in = socket_read($socket, 2048, PHP_BINARY_READ)) {
echo $in;
}
echo "<br><br>";
$out = "<quit />\0";
$in = '';
echo "SEND: $out <br>";
socket_write($socket, $out, strlen($out));
echo "close socket ...<br>";
socket_close($socket);
echo "OK.<br><br>";
?>
|
You're still getting a error in the while loop because the socket is only open for 2 sec and then the while loop ends with a socket_read error.
If you know exactly the response lenght you can also make a single statement of socket_read instead of the while loop to work around the warning message.
Nevertheless the result of the experiment is bad for me, because I had a major error in my idea.
A PHP script called via HTTP is stateless and not designed to have a infinite loop running which listens to a socket. The output of a PHP script is sent to the browser once the script finished sucessfully. But I planned to have is running over a longer period of time until a user quits the session.
So PHP works only if you want to build something on server side where a script but run in a infinite loop.
If somebody has a idea how I could write a socket client with PHP please tell me.
My current ideas are:
- trying out something with AJAX (asynchronouse Javascript and XML) and HTTP polling to a PHP script , which then connects to the socketserver to refresh the client. AJAX is able to update the browser content without a reload. (see this chat example: http://www.linuxuser.at/chat/index.html
- building a hidden frame (technique seems to be called COMET) where a infinite loop runs to request the data from the socket server and give them to another frame whee it is shown. But don't know how to do that.
Why do I try that stuff. I'll try to build a little multiplayer game, but I have no clue about Flash. So I need to figure out something in PHP or Javascript.
There are already good games with AJAX around, see this german example: http://www.travianer.de/ (Just click around on the map to see the character moving around.
Regards, Mike
|