Sockets - Get Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Sockets - Get Data

Post 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?
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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);
?>
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post 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
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

Bump

-Neo
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post 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
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

bump
Post Reply