Link to home
Start Free TrialLog in
Avatar of pugandjody
pugandjody

asked on

connect

I was wondering how to connect to a server (eg. router.limewire.com:6346), send a string to the server then print the resonse to the screen?
ASKER CERTIFIED SOLUTION
Avatar of snoyes_jw
snoyes_jw
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pugandjody
pugandjody

ASKER

I tried using fsockopen, but can't seem to get it right...

I would like to connect to 127.0.0.1:6346, send the string: GNUTELLA CONNECT/0.4\n\n
then take whatever the server sends back and print it to the screen.. could you write that for me?
Heres what I have:

<?php
$fp = fsockopen("24.209.52.3", 6346, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
   $out = "GNUTELLA CONNECT/0.4\n\n";

   fwrite($fp, $out);
   while (!feof($fp)) {
       echo fgets($fp, 128);
   }
   fclose($fp);
}
?>

It works fine, it connects, but it doesnt say what the server returns... i need it to write the server's response to the screen.
Does the script end before timing out?  I've had some problems with some servers not returning an end-of-file character, so the fgets doesn't finish and the while loop is never exited.
How do I fix the timeout problem?
First off, make sure the server terminates the string properly.  Second, use stream_set_timeout, so that if it doesn't finish reading within a few seconds, the read attempt ends and the script continues.

http://www.php.net/manual/en/function.stream-set-timeout.php