I'm implementing websockets for a chat system. The server.php is being invoked via ssh with php /www/server.php
, and the first time it executes fine; but if the process is stopped (ctrl+z), this error is displayed after trying to invoke php /www/server.php
again:
Warning: socket_bind(): unable to bind address [48]: Address already in use in /www/server.php
These are the contents of the /www/server.php file:
<?
$host = '10.10.0.103';
$port = '1337';
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
// ... etc
So, I have two questions:
Can I reuse the same address / port after stopping the
php /www/server.php
job with a FLAG? Isn't SO_REUSEADDR supposed to reuse the same address/port?What are the best practices for this issue? Restarting the websockets server could be an everyday task, e.g.: after updating the contents of /www/server.php
Thanks for any tips!