Page 1 of 1

sockets

Posted: Tue Dec 27, 2005 6:25 pm
by redduck666
ok i could use some help with sockets:


Code: Select all

<?php
set_time_limit (0);
$address = '212.72.100.21';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
socket_listen($sock);
$client = socket_accept($sock);
$input = socket_read($client, 1024);
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);
socket_write($client, $output);
socket_close($client);
socket_close($sock);
?>
if i upload this to my site and execute it in my browser a socket that is listening on port 9000 should be created? is a ssh access a **must** have to create sockets?


also how can i test it is a socket is connectable? ie if my host doesn`t have some kind of firewalling service set that blocks it.


any help appreciated

Posted: Tue Dec 27, 2005 7:30 pm
by josh
well since you set the time limit to 0 once you run it, it will continue running listening on that port, you should implement at least some kind of time out before you start running that



try out the example script here:
http://us2.php.net/sockets
it should work "out of the box" (with the exception of changing the IP)

having ssh access has nothing to do with creating a socket

Posted: Wed Dec 28, 2005 3:54 am
by redduck666
having ssh access has nothing to do with creating a socket
cool

well since you set the time limit to 0 once you run it, it will continue running listening on that port, you should implement at least some kind of time out before you start running that
k will do.
try out the example script here:
http://us2.php.net/sockets
it should work "out of the box" (with the exception of changing the I
actually i tried those, the second example script works just fine, the first one generates an error:

Code: Select all

Warning: socket_bind() unable to bind address [98]: Address already in use in /home/fs666fs/public_html/test.php on line 18
with '@' that error is ignored. the only two differences i made are the change of ip and i commeneted out set_time_limit.

and this is taken from my console, i`m don`t knopw how to interpret it :|

Code: Select all

knoppix@ttyp2[knoppix]$ telnet 212.72.100.21 10000
Trying 212.72.100.21...
telnet: Unable to connect to remote host: Connection timed out

is this bad?

Posted: Wed Dec 28, 2005 6:04 am
by timvw
The error message is pretty clear, not? It says there is already a process (program) that is using that port.. therefore you can't use it anymore.. Normally it takes a while before the port becomes available again (say 10 seconds after the process that was using it dies, the port will become available again).

With @ the error is not ignored, it simply is suppressed. But it doesn't mean that the error/warning message isn't valid anymore..

If there is nothing that accepts connections on that host:port then that is normal (Notice your code wants to connect to port 10 000 and your code shows that you're using 9000.

Other things you might want to try:
- Do i have a network connection to that host? (ping / arping can help here)
- Does the host allow incoming connections on that port? (checking the firewall rules may help)

Posted: Wed Dec 28, 2005 9:58 am
by redduck666
The error message is pretty clear, not? It says there is already a process (program) that is using that port..
in that case there is a trouble, i tried changing 10000 to 65000 and to random ports such as 59367, yet i got the same error, so i`m kinda confused :( (i find it HIGHLY unlikelly that programs outside this code are using those ports)

If there is nothing that accepts connections on that host:port then that is normal (Notice your code wants to connect to port 10 000 and your code shows that you're using 9000.
i was talking about the code example on php.net/sockets, and it (that code) uses the port 10000.
Do i have a network connection to that host? (ping / arping can help here)
the server drops ping requests. according to packages.gentoo.org arping is "A utility to see if a specific IP address is taken and what MAC address owns it ", however i`m kinda stuck on knoppix live cd and can`t install utility to arping my site.

- Does the host allow incoming connections on that port? (checking the firewall rules may help)

that is one of the things i`m trying to find out :| the other one is how to open a socket on my site.

Posted: Wed Dec 28, 2005 12:00 pm
by josh
Doubt you're gonna find an easy way to check the firewall rules with no SSH. Call your host up and ask them

Posted: Wed Dec 28, 2005 3:05 pm
by redduck666
Doubt you're gonna find an easy way to check the firewall rules with no SSH. Call your host up and ask them


thanks, will do. if there is a firewall i am screwed up? any ways around this?

Posted: Wed Dec 28, 2005 3:22 pm
by josh
ask your host to open up the port you need

Posted: Thu Dec 29, 2005 6:40 am
by BDKR
There are some very obvious problems here that need to be addressed.

1) Why do you want to open a socket in the first place? Especially since network communication is allready taken care of at the browser level. Besides, I doubt Apache, which is PHP's [/I]pwn3r[/I] in this case, is going to let arbitrary scripts open sockets beyond it's own mandate. Remeber, Apache at the very core is a socket server itself.

2) Scripts run server side in Browsers won't stay open forever without a negative effect on some level. Even if you set the time limit to 0, it would die when the window is closed. Nevermind the fact that Apache would probably (hopefully) intervene at some point.

3) Your host having a firewall is a good thing! Don't try to circumvent it if you really don't understand it.

If you need to open a socket up for some reason that is prompted by a visit to a web page, you would do best to have a seperate CLI type script that can be called via exec() or system() that then detaches and runs on it's own. In that way, you can at least close the browser window without effecting the script you've written.

What is the purpose for all this anyways?

Posted: Thu Dec 29, 2005 8:06 am
by redduck666
jshpro2 wrote:ask your host to open up the port you need
the refuse to do so, i guess i`m screwed up :(

What is the purpose for all this anyways?
the purpose of this was to creat a php scipt that would connect to sql server and than listed for orders, on my side i planed to create a java irc bot using pircbot, the result -- "!search *username*" on irc channel would read the sql db write info about that user.


thanks for the help guys

Posted: Thu Dec 29, 2005 9:28 am
by BDKR
redduck666 wrote: the purpose of this was to creat a php scipt that would connect to sql server and than listed for orders, on my side i planed to create a java irc bot using pircbot, the result -- "!search *username*" on irc channel would read the sql db write info about that user.
IRC eh? I can see why the host wouldn't allow it for this reason alone.

Your best bet would be to just setup a server at home.

Posted: Thu Dec 29, 2005 10:03 am
by josh
BDKR, there is always ignore_user_abort() and I can go on and on about with scenarios in which using sockets are the only situation, however I would agree yes it is probably "cleaner" to put your socket code into a PHP shell script and call it via exec() or the likes sometimes this does not make sense.

I also must add if you are going to have your script opening sockets on a time limit = 0 script (sockets that are listening and serving the purpose of some kind of server) and ignoring the user abort, you are better off writing a multi client socket server in a shell script in every case that I can think of.

Posted: Thu Dec 29, 2005 11:38 am
by BDKR
jshpro2 wrote: BDKR, there is always ignore_user_abort() and I can go on and on about with scenarios in which using sockets are the only situation, however I would agree yes it is probably "cleaner" to put your socket code into a PHP shell script and call it via exec() or the likes sometimes this does not make sense.
Of course there are scenarios where using sockets are most ideal, but not where the browser is also in play.
jshpro2 wrote: I also must add if you are going to have your script opening sockets on a time limit = 0 script (sockets that are listening and serving the purpose of some kind of server) and ignoring the user abort, you are better off writing a multi client socket server in a shell script in every case that I can think of.
Of course.

Posted: Thu Dec 29, 2005 11:39 am
by redduck666
IRC eh? I can see why the host wouldn't allow it for this reason alone.
you ain`t the only one :(

Your best bet would be to just setup a server at home.

my biggest concern is that my site will be loading slow. can you make any estimation of what is the difference in the loading times if i am the only person browsing my site (ie no-one on the internet knows that it has been set up) or if it functions properly (people that would usually visit it are actually visiting it)? my upload is 512 kb/s and my site gets 20-30k visits/month and phpbb`s index.php (on current host) is generated in like 1.9 seconds.