Sockets please look

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
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Sockets please look

Post 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);

?>
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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?
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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?
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 ;)
Nick22
Forum Newbie
Posts: 11
Joined: Fri Nov 04, 2005 10:57 am

Post 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
Post Reply