NotionCommotion Posted December 29, 2016 Share Posted December 29, 2016 I am getting the following error. It is caused when I first bind a connection, then get some error halting the code, and then try the code again, but it is already bound. [Michael@devserver datalogger]$ sudo netstat -lnptu | grep 1337 tcp 0 0 192.168.1.200:1337 0.0.0.0:* LISTEN 3899/httpd [Michael@devserver datalogger]$ My solution has been to reboot the machine or kill -9 3899. Instead, can I if necessary first check if tcp://192.168.0.100:1337 is already bound, and then unbind it using PHP, and then proceed? I thought $socket->shutdown() might work, but nope. <?php require 'vendor/autoload.php'; use React\EventLoop\Factory; use React\Socket\Server as SocketServer; use React\Http\Server as HttpServer; $loop = Factory::create(); $socket = new SocketServer($loop); //$socket->shutdown(); //Close if currently open $socket->on('connection', function($conn) {/* ... */}); $socket->listen(1337,'192.168.0.100'); [Michael@devserver react]$ php xxx.php PHP Fatal error: Uncaught exception 'React\Socket\ConnectionException' with message 'Could not bind to tcp://192.168.0.100:1337: Cannot assign requested address' in /var/www/react/vendor/react/socket/src/Server.php:29 Stack trace: #0 /var/www/react/xxx.php(13): React\Socket\Server->listen(1337, '192.168.0.100') #1 {main} thrown in /var/www/react/vendor/react/socket/src/Server.php on line 29 Fatal error: Uncaught exception 'React\Socket\ConnectionException' with message 'Could not bind to tcp://192.168.0.100:1337: Cannot assign requested address' in /var/www/react/vendor/react/socket/src/Server.php:29 Stack trace: #0 /var/www/react/xxx.php(13): React\Socket\Server->listen(1337, '192.168.0.100') #1 {main} thrown in /var/www/react/vendor/react/socket/src/Server.php on line 29 [Michael@devserver react]$ Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/ Share on other sites More sharing options...
Jacques1 Posted December 29, 2016 Share Posted December 29, 2016 SO_REUSEADDR Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540859 Share on other sites More sharing options...
requinix Posted December 29, 2016 Share Posted December 29, 2016 It won't help for particularly bad halting errors, but you can use register_shutdown_function to create a failsafe that closes the connection (if not already closed). But... what kind of error is terminating your code yet leaving the process running? Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540862 Share on other sites More sharing options...
NotionCommotion Posted December 29, 2016 Author Share Posted December 29, 2016 But... what kind of error is terminating your code yet leaving the process running? Maybe a silly mistake, and had another script running? I didn't think so, but starting to think so. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540876 Share on other sites More sharing options...
kicken Posted December 29, 2016 Share Posted December 29, 2016 I would suggest your not run your server scripts via apache. You're adding extra layers of complexity by doing that which may cause issues you wouldn't have otherwise. Run your scripts via the CLI interpreter instead. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540878 Share on other sites More sharing options...
NotionCommotion Posted December 30, 2016 Author Share Posted December 30, 2016 I would suggest your not run your server scripts via apache. You're adding extra layers of complexity by doing that which may cause issues you wouldn't have otherwise. Run your scripts via the CLI interpreter instead. I never said I ran such a script via Apache, but you guessed correct, and believe that was the cause! I occasionally do so for IDE debugging purposes only. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540885 Share on other sites More sharing options...
requinix Posted December 30, 2016 Share Posted December 30, 2016 I never said I ran such a script via Apache,Actually you kinda did: the output from netstat in your first post shows httpd, which is Apache. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540886 Share on other sites More sharing options...
NotionCommotion Posted December 30, 2016 Author Share Posted December 30, 2016 Actually you kinda did: the output from netstat in your first post shows httpd, which is Apache. Ah ha! I was about to question kicken about his clairvoyance, but then saw your post. I noticed the httpd, but incorrectly thought it was some sort of reactphp http server that the script in question was attempting to create. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540887 Share on other sites More sharing options...
kicken Posted December 30, 2016 Share Posted December 30, 2016 I occasionally do so for IDE debugging purposes only. You should be able to use debugging in a CLI context as well. You'll have to look into your IDE's settings to determine how to set it up. With PHPStorm you'd go to the run/debug configuration and add a configuration for 'PHP Script'. After the configuration is added you can then just select that configuration and hit the debug button to run it. Quote Link to comment https://forums.phpfreaks.com/topic/302831-unbinding-an-address/#findComment-1540888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.