I had a server script running and I closed it prematurely using Ctrl+Z. When I tried to run it again it tells me that the port 1001 is already in use. In a live environment there is always the risk that the script could crash or something else may trigger it to end early. How do I automatically unbind this port before connecting to it? I am going to use a port that I know nothing else will ever use, so its safe to unbind it automatically.
Currently my server script is basic.
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
1001
);
$server->run();
I followed the guide found here http://socketo.me/docs/hello-world . Everything has worked as expected, I'mm just stuck with this issue now.
I am running PHP 5.4 on Centos 7.
I couldnt find any way to do this through the command line. Only solution i could see, which could be trouble if someone finds out is to kill the script by sending a message to the websocket. Every message to the websocket server has an "action". A switch will then do something for each action. Create a random action thats impossible to guess. Within this switch use die();
. This will kill the script and unbind the port so you dont need to go finding the PID and killing that manually when the server needs restarted.
This can be used to make a quick restart .sh script. Use php to make a websocket call to kill the server and call if from a bash script, then call php server.php and away you go.