thundium
thundium

Reputation: 1053

Program doesn't stop when "ctrl c" is used to terminate program

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

Answers (1)

M. Adel
M. Adel

Reputation: 407

Use this in the bash script (before calling your binary):

trap "fuser -k -n tcp 1234 && exit" SIGINT SIGTERM

Upvotes: 1

Related Questions