Reputation: 1053
I have a programme (C++ compiled) that using tcp socket to communicated. The programme is configured in two mode. Let's say mode A and mode B.
Start the programme mode A, it will give some prints like:
waiting connections on port 1234
local endpoint : 0.0.0.0:1234
//I think it is using boost for TCP socket
Then start mode B. They will find each other and run perfect.
Question is if I start mode A, and then use "ctrl c" to terminate the application with mode A. It will left the port open there.
When I start the mode B, it will also find the connection and runs with error due to A is not there.
I have a bash to run the application, I want to ask how I can force that port to close? (In bash or other possible way)
Thanks
Upvotes: 2
Views: 709
Reputation: 407
Use this in the bash script (before calling your binary):
trap "fuser -k -n tcp 1234 && exit" SIGINT SIGTERM
Upvotes: 1