2

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

user207421
  • 305,947
  • 44
  • 307
  • 483
thundium
  • 995
  • 4
  • 12
  • 30
  • If you use `netstat -a -n`, what do you see in the output for all the columns? Is the State listed as `TIME_WAIT` by chance in these conditions? – Jeff May 21 '14 at 11:57
  • Hi @Jeff It shows like:tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN – thundium May 21 '14 at 11:59
  • Can you modify the C++ code? It would be better if that program cleaned up after itself before exiting. – chepner May 21 '14 at 12:00
  • Hi @chepner yes, I can modify the C++ code and re-complile it. I was thinking C++ application can not do that due to ctrl c terminated the entir programme. Do you have any suggestion ? – thundium May 21 '14 at 12:10
  • @thundium Ctrl-c causes your program to receive `SIGINT`, which it can catch, allowing it to run some code before exiting. You may be thinking of `SIGKILL` (`kill -9`), which cannot be caught and forces your program to exit immediately. – chepner May 21 '14 at 12:16
  • Hi @M.Adel I tried your command in the console which close the port indeed. But not yet make it working by adding into my bash script. (It is not a simple bash script and I guess i need find some neat solution) Thanka a lot. I'll do more effort on updaing our bash script. – thundium May 21 '14 at 12:21
  • You just need to add the mentioned line at the top of the script, e.g. after #!/bin/bash, it'll do what you need and handle the CTRL+C and the SIGTERM. – M. Adel May 21 '14 at 12:24
  • @M.Adel I see there is trap 'cleanup' SIGTERM SIGHUP SIGINT in the bash, and I added "fuser -k -n tcp 1234" in the beginning of function cleanup. But it is not working – thundium May 21 '14 at 12:26
  • I'm not sure if you can handle the signals with a function, but you can try replacing 'cleanup' with "fuser ..." – M. Adel May 21 '14 at 12:29
  • I would still go with fixing the C++ code to handle SIGINT properly instead of patching it up in the launch script. I found this http://stackoverflow.com/questions/4250013/is-destructor-called-if-sigint-or-sigstp-issued that explains how to make your C++ close in a way that makes your destructors run. – r_ahlskog May 21 '14 at 12:31

1 Answers1

1

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

trap "fuser -k -n tcp 1234 && exit" SIGINT SIGTERM
M. Adel
  • 401
  • 5
  • 7
  • This would be a reasonable attempt to cleanup after an errant program if and only if the OP didn't have the source for the program. This should not be used in place of proper handling of SIGINT and SIGTERM in the OP's binary. – msw May 21 '14 at 13:26