Closing off sockets in PHP

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Closing off sockets in PHP

Post by impulse() »

I've just ran this code:

Code: Select all

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_bind($socket, '1.1.1.1', 1);
socket_listen($socket);

while ($connection = socket_accept($socket)) {
  for ($i = 0; $i < 10000; $i++) {
    socket_write($connection, "Go away");
  }
}
And when the user that connects to the the script exists it leaves the port open. If I try and run the same script again I get the following message
PHP Warning: socket_bind(): unable to bind address [98]: Address already in use in /var/www/html/php/test/socketread.php on line 5
Although if I try and telnet to myself on the port it doesn't seem to be open.
Last edited by impulse() on Tue Nov 07, 2006 8:23 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not seeing a socket_close().
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I've had this same issue before. Leaving it for a minute or two gets it working again but it's very confusing why PHP sees it as a bound socket because you're right, when you look into it it's not actually bound at all.
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Well, the way I see it, your script may have bound to the ip/port one time, and since you never closed the socket (note: check feyd's post), you can't bind to it again, unless, you restart your machine, or wait some x time for it to unbind. There's probably a program to unbind addresses but I don't know any.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

If your using bind and doing something that is not normal, like exiting a bind socket then the socket will remain in a bind state until the timeout is encounter. Bind within a script should only be used for certain applications, and surly not when doing what your example does. If you must do non-standard socket coding then use socket_set_option() to test if the socket is in a bind state and if it is, set the REUSE flag so it can be freed and used again. But as feyd said, socket_close(), should always be used for any socket stream.


printf
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Even better:

Code: Select all

socket_shutdown($socket, 2);
socket_close($socket);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

printf wrote:set the REUSE flag so it can be freed and used again.
I agree that setting SO_REUSEADDR is essential... (Otherwise it will keep a serious while before the socket becomes available again, even if you closed it...)
Post Reply