Page 1 of 1

Sockets - Get Data

Posted: Tue Apr 06, 2004 12:31 pm
by NeoPuma
Hey!
I'm trying to make an site using sockets, I've got a connection, and now I want to get data.

I've attempted but got the error:
Warning: socket_read(): supplied resource is not a valid Socket resource in /users/silentdr/public_html/mjs-host/~neopuma/srvplayers.php on line 11
I have no idea, but i tryed to take an example and alter it abit, doing so I got this:

Code: Select all

<?php

$fp = fsockopen("IP REMOVED FOR SAFTEY", 3000, $errno, $errstr, 30);

if (!$fp) {

echo 'A error has occured - The server is currently offline!';

} else {

$data = socket_read($fp, 3000, PHP_NORMAL_READ);

$stats = explode("/", $data);

str_replace("-;", "", $stats[3]);

echo "Server: " .$stats[3]. " - Online - Players online: " .$stats[5]. "";

   }
   fclose($fp);
}

?>
All help would be really great! Thanks

-Neo

p.s: It can take upto 20seconds for data to arrive - how can i keep it connected untill i recive data?

Posted: Tue Apr 06, 2004 1:22 pm
by hedge
from the docs at php.net it looks like the socket_read function only understands a socket opened with socket_create. You're mixing extensions.

to read from a fsockopen handle use fgets like this

Code: Select all

<?php
       while (!feof($fp)) $buf .= fgets($fp,1024);
?>

Posted: Tue Apr 06, 2004 2:26 pm
by NeoPuma
Thanks.

So I now got:

Code: Select all

<?php

$fp = fsockopen("141.149.139.101", 3000, $errno, $errstr, 30);

if (!$fp) {

echo 'A error has occured - The server is currently offline!';

} else {

while (!feof($fp)) $data .= fgets($fp,1024);

$stats = explode("/", $data);

str_replace("-;", "", $stats[3]);

echo "Server: " .$stats[3]. " - Online - Players online: " .$stats[5]. "";

   }
   fclose($fp);

?>
But - The page never seems to load.
Heres the url for the output etc: http://www.mjs-hosting.co.uk/~neopuma/srvplayers.php

If any one could take a look plz tht would be great!

-Neo

Posted: Wed Apr 07, 2004 4:35 am
by NeoPuma
Bump

-Neo

Posted: Wed Apr 07, 2004 5:06 am
by kettle_drum
Well unless the remote server disconects the socket then the while statement will continue to loop. If the server sends the info you want in the first fgets, then you dont have to bother with the while loop.

If it doesnt, then place all the parsing within the while loop so that you check to see if you have the info and then if you do exit the while loop and close the socket.

Posted: Wed Apr 07, 2004 5:54 am
by NeoPuma
hmmm, im sorry i dont understand you.
Any way, I've talking to my mate on IRC, and he's told me to edit the code and i ended up with this:

Code: Select all

<?php

$fp = fsockopen("213.106.51.225", 3000, $errno, $errstr, 30);

if (!$fp) {

echo 'A error has occured - The server is currently offline!';

} else {

while (!feof($fp))  {
$data .= fgets($fp,1024);

$stats = explode("/", $data);

str_replace("-;", "", $stats[3]);

echo "Server: " .$stats[3]. " - Online - Players online: " .$stats[5]. "";

   }
   fclose($fp);

}

?>
Now, if you go back to http://www.mjs-hosting.co.uk/~neopuma/srvplayers.php the page wont load. Try telneting to that IP on that port, and you'll see data is automaticly sent out. So, Any help please? :(

-Neo

Posted: Wed Apr 07, 2004 11:50 am
by NeoPuma
bump