Page 1 of 1

Sockets please look

Posted: Fri Nov 04, 2005 11:02 am
by Nick22
I need help, I am trying to connect to an ip on port 3000 and receive its data.

I run this code and nothing happens.. it appears to be connecting but nothing displays.. page doesn't even display.

my code

Code: Select all

<?php
$fp = fsockopen('82.148.121.13',3000,$errno,$errdesc,10);
echo "Trying $host<br>\n";
if(!$fp){
echo("couldnt connect to $host");
echo "<br><hr><br>\n";
}
echo "trying to get $page<br>\n";

echo fgets($fp, 1024);
echo "<br><br><br>\n";

fclose($fp);

?>

Posted: Fri Nov 04, 2005 11:14 am
by yum-jelly
You need to send a request! The function fsockopen() only opens the connection socket! The server does not send anything until it knows it has a pending request! Have a look at the fsockopen() to better understand this!

yj

Posted: Fri Nov 04, 2005 11:34 am
by Nick22
If you connect using telnet it receivs data without sending any data.. er maybe php needs data to be sent anyway before it can start receiving?

Posted: Fri Nov 04, 2005 11:44 am
by yum-jelly
You need to at least send EOL -> \r?\n for any sub-standard port (udp, telnet), and a full request for http ports, or PHP will not even read into the socket!

yj

Posted: Fri Nov 04, 2005 11:47 am
by Nick22
Ok lets get me connected first lol..

Code: Select all

<?php
    $fp = fsockopen('212.146.85.6', 3000,$errno,$errstr, 4);
         if (!$fp){
            echo "online";
         } else {
		echo "offline";
             fclose($fp);
         }
?>
works in telnet but not here.. I get;

Warning: fsockopen(): unable to connect to 212.146.85.6:3000 in /home/woool/public_html/serverlist/index.php on line 2
online

Posted: Fri Nov 04, 2005 12:01 pm
by Nick22
Ok it works if I use google.com as host and port 80..

could it be my host blocks all other ports except port 80?

EDIT: just asked host they said they can't unblock the ports.. does that make sense or what? I pay for hosting and they can't do that? Is there really security risks with allowing out going connections?

Posted: Fri Nov 04, 2005 1:33 pm
by Nick22
Ok I am now running my own webserver.... connecting works fine.. but how is reading the data done? What did you mean by EOL -> \r?\n do I send that first?

Code: Select all

<?php
$fp = fsockopen( "212.146.85.6", 3000, $errno,
$errdesc); //establish connection

fputs($fp, "EOL -> \r?\n");

while(!feof($fp)){
echo fgets($fp, 1024);
}
fclose($fp); //close connection
?>
Thanks for your help!

Posted: Fri Nov 04, 2005 1:37 pm
by timvw
I prefer a block everything all, allow only the minimum required policy for a production server..

Offcourse they can unblock ports, they just don't want to do it.. And if you have a good sysadmin you'll have to convince him why you need to be able to do that.. That's their job after all :)

Posted: Fri Nov 04, 2005 1:40 pm
by timvw
If you want to retrieve "pages" i suppose you're trying to talk HTTP..

You can find a good description, examples of a typical converstation, specifications (RFC) at http://en.wikipedia.org/wiki/HyperText_ ... r_Protocol.

Posted: Fri Nov 04, 2005 1:43 pm
by Nick22
No i'm trying to receive data wich will look like this 0/0/0/4/osp then i'll parse it.. If I connect via telnet it doesn't need to send anything.. the server automaticly sends data to me on a set timer.

Open up telnet and type Open 212.146.85.6 3000

and you will see what I mean make sure you wait 5 seconds.

Posted: Fri Nov 04, 2005 2:37 pm
by timvw
I know how sockets work ;) They are simply datastreams... Just as you read/write a file ;)

As an example, here is my Banner server ;)
http://timvw.madoka.be/programming/java/banner.txt

The service you are connecting to, doesn't close the connection.. So, your php script will take a while to read up to 1024 bytes ;)

Posted: Fri Nov 04, 2005 2:49 pm
by Nick22
Ok I figured it out.. its because it doesn't close the connection so it just times out basicly..

How would I make it read for only a set interval or intill it retrieves some data.?


Thanks