Jump to content

Recommended Posts

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]$

 

Link to comment
https://forums.phpfreaks.com/topic/302831-unbinding-an-address/
Share on other sites

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?

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.

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.

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.

 

post-124404-0-92340400-1483062012_thumb.png

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.